Following exercises are originally published on Advanced R website.

  1. What are the six types of atomic vectors? How does a list differ from an atomic vector?

  2. What makes is.vector() and is.numeric() fundamentally different to is.list() and is.character()?

  3. Test your knowledge of vector coercion rules by predicting the output of the following uses of c():

c(1, FALSE)
c("a", 1)
c(list(1), "a")
c(TRUE, 1L)
  1. Why do you need to use unlist() to convert a list to an atomic vector? Why doesn’t as.vector() work?

  2. Why is 1 == “1” true? Why is -1 < FALSE true? Why is “one” < 2 false?

  3. What does dim() return when applied to a vector?

  4. If is.matrix(x) is TRUE, what will is.array(x) return?

  5. How would you describe the following three objects? What makes them different to 1:5?

x1 <- array(1:5, c(1, 1, 5))
x2 <- array(1:5, c(1, 5, 1))
x3 <- array(1:5, c(5, 1, 1))
  1. What attributes does a data frame possess?

  2. What does as.matrix() do when applied to a data frame with columns of different types?

  3. Can you have a data frame with 0 rows? What about 0 columns?

  4. Fix each of the following common data frame subsetting errors:

mtcars[mtcars$cyl = 4, ]
mtcars[-1:4, ]
mtcars[mtcars$cyl <= 5]
mtcars[mtcars$cyl == 4 | 6, ]
  1. Why does mtcars[1:20] return an error? How does it differ from the similar mtcars[1:20, ]?

  2. What does df[is.na(df)] <- 0 do? How does it work?