Coding basics - Extra Practice
These questions practice the R coding basics from the Taxonomy of Data tutorial: arithmetic, assignment, object classes, functions, vectors, data frames, and knowing when R prints output.
Arithmetic
- What does R return?
4 + 7 * 2Show answer
R returns 18. Multiplication happens before addition: 7 * 2 is 14, then 4 + 14 is 18.
- What does R return?
(4 + 7) * 2Show answer
R returns 22. Parentheses happen first: 4 + 7 is 11, then 11 * 2 is 22.
- What does R return?
2 ^ 3 + 1Show answer
R returns 9. Exponents happen before addition: 2 ^ 3 is 8, then 8 + 1 is 9.
- Write one line of R code that computes the number of minutes in 3.5 hours.
Show answer
3.5 * 60This returns 210.
- Write one line of R code that computes the natural log of 100.
Show answer
log(100)By default, log() computes the natural logarithm.
- What does R return?
sqrt(81) + 2 ^ 3Show answer
R returns 17. sqrt(81) is 9 and 2 ^ 3 is 8, so the sum is 17.
- What does R return?
10 / 2 + 3 * 4Show answer
R returns 17. Division and multiplication happen before addition: 10 / 2 is 5, 3 * 4 is 12, and 5 + 12 is 17.
Assignment and Changing Values
- After running these lines, what is stored in
x?
x <- 5
x <- 8Show answer
x stores 8. The second line just overwrites what was previously stored in x.
- After running these lines, what is stored in
total?
coffee <- 20
snack <- 6
total <- coffee + snackShow answer
total stores 26.
- After running these lines, what is stored in
total?
coffee <- 20
snack <- 6
total <- coffee + snack
snack <- 10Show answer
total still stores 26. Changing snack later does not automatically rerun the earlier assignment to total.
- After running these lines, what is stored in
total?
coffee <- 20
snack <- 6
total <- coffee + snack
snack <- 10
total <- coffee + snackShow answer
total stores 30. The final line reruns the calculation using the current values of coffee and snack.
- Which of these are valid object names in R?
budget
budget_2026
2026_budget
my-budget
coffee2Show answer
Valid names: budget, budget_2026, and coffee2.
Invalid names: 2026_budget starts with a number, and my-budget uses -, which R reads as subtraction.
- Write code that stores the value
1800in an object namedrent, then stores five times this valuesemester_rent.
Show answer
rent <- 1800
semester_rent <- rent * 5After this code, semester_rent stores 9000.
- After running these lines, what is stored in
aandb?
a <- 3
b <- a + 4
a <- 10Show answer
a stores 10 and b stores 7. The assignment to b used the old value of a, and b does not update automatically when a changes.
Object Classes and Data Types
- What class will R report?
class(42)Show answer
R reports "numeric".
- What class will R report?
class("42")Show answer
R reports "character". The quotation marks make "42" a string, not a number.
- What happens if you run this code?
"42" + 8Show answer
R gives an error because "42" is a character string. Arithmetic works on numbers, not strings. It’s like trying to to add “apple” to the number 6. It makes no sense.
- What class will R report?
class(c("Adelie", "Gentoo", "Chinstrap"))Show answer
R reports "character". Even though this is a vector containing many strings, the class function just tells you what kind of “stuff” is in the vector. It doesn’t tell you that it’s a vector.
- What class will R report?
species <- factor(c("Adelie", "Gentoo", "Chinstrap"))
class(species)Show answer
R reports "factor". The factor() function turns the character vector into a factor.
- What levels are stored in
rating?
rating <- factor(
c("good", "poor", "excellent", "mid"),
levels = c("poor", "mid", "good", "excellent")
)Show answer
The levels are "poor", "good", and "excellent", in that order.
- What will the resulting dataframe look like?
df <- data.frame(name = c("Ava", "Ben"), score = c(9, 7))Show answer
| name | score |
|---|---|
| Ava | 9 |
| Ben | 7 |
Functions and Their Responses
- Identify the function, input, and output.
sqrt(49)Show answer
The function is sqrt(), the input is 49, and the output will be 7.
- Identify the function(s), input(s), and output(s).
mean(c(10, 20, 30))Show answer
R returns 20.
There are two functions being called here:
cwhich is given three inputs: 10, 20, and 30. It returns a vector with these numbers in it.- That vector becomes the one input to the
meanfunction, which returns the average of all the numbers in that vector (so, 20 here).
- Identify the function(s), input(s), and output(s).
length(c("red", "blue", "green", "blue"))Show answer
R returns 4, the number of elements in the vector.
There are two functions being called here:
cwhich is given four inputs, each of which is a string, and returns a vector with these strings as an output.- This vector becomes the input for
length, which just counts how many things are in the vector (4 here).
- Write one line of code that simultaneously calculates the square root of each of these numbers: 16, 25, and 36.
Show answer
sqrt(c(16, 25, 36))R returns c(4, 5, 6).
This one is different because of how sqrt works. If you give it just a number (e.g., sqrt(4)) it will give you one number back (2). But if you give it a vector of numbers, it will return a vector with the square roots of all the numbers.
- What does R return?
c(1, 2, 3) * 10Show answer
R returns c(10, 20, 30). Similar to sqrt, when multiplication * is used on a vector, R multiplies once for each value in the vector.
- What does R return?
c(1, 2, 3) + c(10, 20, 30)Show answer
R returns c(11, 22, 33). When things like + or * are given two vectors, it returns a vector of the result for each corresponding pair.
- What class will R report?
mixed <- c(1, "two", 3)
class(mixed)Show answer
R reports "character". A vector must store one basic type, so R converts the numbers to character strings.
Data Frames
- Create a data frame called
petsthat has information about three different pets: a 4-year-old cat named Milo, a 2-year-old dog named Luna, and a 7-year-old cat named Nori. Also think about what the final dataframe will look like.
Show answer
pets <- data.frame(
name <- c("Milo", "Luna", "Nori")
age <- c(4, 2, 7)
species <- factor(c("cat", "dog", "cat"))
)Or, perhaps more legibly:
name <- c("Milo", "Luna", "Nori")
age <- c(4, 2, 7)
species <- factor(c("cat", "dog", "cat"))
pets <- data.frame(name, age, species)The resulting dataframe will look like:
| name | age | species |
|---|---|---|
| Milo | 5 | cat |
| Luna | 2 | dog |
| Nori | 7 | cat |
- In the
petsdata frame from the previous question, what is the unit of observation?
Show answer
The unit of observation is one pet. Each row stores information about one pet.
- What happens if you try to create this data frame?
data.frame(
name = c("A", "B", "C"),
score = c(10, 12)
)Show answer
R gives an error because the columns have different lengths. There are three names, but only two scores, so R doesn’t have a full table of data. All rows have to have the same number of columns.
- Write code to create this data frame.
| city | temperature | weather |
|---|---|---|
| Berkeley | 64 | cloudy |
| Oakland | 67 | sunny |
| Richmond | 63 | foggy |
Make city and weather character vectors and temperature numeric.
Show answer
city <- c("Berkeley", "Oakland", "Richmond")
temperature <- c(64, 67, 63)
weather <- c("cloudy", "sunny", "foggy")
weather_df <- data.frame(city, temperature, weather)- Write code to create this data frame with an ordered factor for
year.
| name | height | year |
|---|---|---|
| Leia | 160 | sophomore |
| Luke | 170 | freshman |
| Han | 182 | senior |
| Lando | 178 | junior |
Show answer
students <- data.frame(
name = c("Leia", "Luke", "Han", "Lando"),
height = c(160, 170, 182, 178),
year = factor(
c("sophomore", "freshman", "senior", "junior"),
levels = c("freshman", "sophomore", "junior", "senior"),
ordered = TRUE
)
)What Prints?
- Which lines print output to the console?
x <- c(5, 2)
x
c(5, 2)Show answer
The second and third lines print output. The assignment line stores a value but does not print it.
- Which lines print output to the console?
scores <- c(10, 20, 30)
mean(scores)
avg <- mean(scores)
avgShow answer
The second and fourth lines print output. The first and third lines are assignments, so they do not print.
- What appears in the console?
answer <- 2 ^ 4Show answer
Nothing prints to the console. The value 16 is stored in answer.
- What appears in the console?
answer <- 2 ^ 4
answerShow answer
R prints 16 on the second line because typing an object’s name asks R to display the object.
- Which lines print output to the console?
species <- factor(c("Adelie", "Gentoo"))
class(species)
levels(species)
penguins <- data.frame(species)Show answer
The second and third lines print output. The first and fourth lines are assignments, so they do not print.
- Write two lines of code that store
c(3, 6, 9)invaluesand then print the mean to the console.
Show answer
values <- c(3, 6, 9)
mean(values)The first line stores the vector. The second line prints 6.
- Write two lines of code that store
c(3, 6, 9)invaluesand store the mean invalues_meanwithout printing the mean.
Show answer
values <- c(3, 6, 9)
values_mean <- mean(values)Both lines are assignments, so neither line prints the mean.
- What class will R report?
resident <- c(TRUE, FALSE, TRUE)
class(resident)Show answer
R reports "logical". TRUE and FALSE are logical values, not character strings.
- What class will R report?
resident <- c("TRUE", "FALSE", "TRUE")
class(resident)Show answer
R reports "character". The quotation marks make these strings, not logical values.