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

  1. What does R return?
4 + 7 * 2
Show answer

R returns 18. Multiplication happens before addition: 7 * 2 is 14, then 4 + 14 is 18.

  1. What does R return?
(4 + 7) * 2
Show answer

R returns 22. Parentheses happen first: 4 + 7 is 11, then 11 * 2 is 22.

  1. What does R return?
2 ^ 3 + 1
Show answer

R returns 9. Exponents happen before addition: 2 ^ 3 is 8, then 8 + 1 is 9.

  1. Write one line of R code that computes the number of minutes in 3.5 hours.
Show answer
3.5 * 60

This returns 210.

  1. Write one line of R code that computes the natural log of 100.
Show answer
log(100)

By default, log() computes the natural logarithm.

  1. What does R return?
sqrt(81) + 2 ^ 3
Show answer

R returns 17. sqrt(81) is 9 and 2 ^ 3 is 8, so the sum is 17.

  1. What does R return?
10 / 2 + 3 * 4
Show 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

  1. After running these lines, what is stored in x?
x <- 5
x <- 8
Show answer

x stores 8. The second line just overwrites what was previously stored in x.

  1. After running these lines, what is stored in total?
coffee <- 20
snack <- 6
total <- coffee + snack
Show answer

total stores 26.

  1. After running these lines, what is stored in total?
coffee <- 20
snack <- 6
total <- coffee + snack
snack <- 10
Show answer

total still stores 26. Changing snack later does not automatically rerun the earlier assignment to total.

  1. After running these lines, what is stored in total?
coffee <- 20
snack <- 6
total <- coffee + snack
snack <- 10
total <- coffee + snack
Show answer

total stores 30. The final line reruns the calculation using the current values of coffee and snack.

  1. Which of these are valid object names in R?
budget
budget_2026
2026_budget
my-budget
coffee2
Show 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.

  1. Write code that stores the value 1800 in an object named rent, then stores five times this value semester_rent.
Show answer
rent <- 1800
semester_rent <- rent * 5

After this code, semester_rent stores 9000.

  1. After running these lines, what is stored in a and b?
a <- 3
b <- a + 4
a <- 10
Show 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

  1. What class will R report?
class(42)
Show answer

R reports "numeric".

  1. What class will R report?
class("42")
Show answer

R reports "character". The quotation marks make "42" a string, not a number.

  1. What happens if you run this code?
"42" + 8
Show 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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

  1. Identify the function, input, and output.
sqrt(49)
Show answer

The function is sqrt(), the input is 49, and the output will be 7.

  1. 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:

  • c which 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 mean function, which returns the average of all the numbers in that vector (so, 20 here).
  1. 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:

  • c which 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).
  1. 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.

  1. What does R return?
c(1, 2, 3) * 10
Show 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.

  1. 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.

  1. 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

  1. Create a data frame called pets that 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
  1. In the pets data 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.

  1. 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.

  1. 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)
  1. 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?

  1. 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.

  1. Which lines print output to the console?
scores <- c(10, 20, 30)
mean(scores)
avg <- mean(scores)
avg
Show answer

The second and fourth lines print output. The first and third lines are assignments, so they do not print.

  1. What appears in the console?
answer <- 2 ^ 4
Show answer

Nothing prints to the console. The value 16 is stored in answer.

  1. What appears in the console?
answer <- 2 ^ 4
answer
Show answer

R prints 16 on the second line because typing an object’s name asks R to display the object.

  1. 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.

  1. Write two lines of code that store c(3, 6, 9) in values and 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.

  1. Write two lines of code that store c(3, 6, 9) in values and store the mean in values_mean without printing the mean.
Show answer
values <- c(3, 6, 9)
values_mean <- mean(values)

Both lines are assignments, so neither line prints the mean.

  1. 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.

  1. 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.