x1 <- list(c(2, 7, 9))
x2 <- list(list(c(2, 7, 9)))
x3 <- list(c(2, 7, 9), list("a", "b"))
x4 <- list(c("a", "b"), list(list(c(2, 7, 9))))Activity: practice with lists
Practice with lists
- Each of the following lists contains the vector
c(2, 7, 9). Determine the correct list indexing syntax to extract that vector from the list (your code should return just the vector, not a list containing the vector).
Solution:
x1[[1]][1] 2 7 9
x2[[1]][[1]][1] 2 7 9
x3[[1]][1] 2 7 9
x4[[2]][[1]][[1]][1] 2 7 9
- Create a list
x5such that the vectorc(2, 7, 9)can be extracted withx5[[3]][[2]][[1]].
Solution: Here is one example:
x5 <- list("a", "b", list("c", list(c(2, 7, 9))))
x5[[3]][[2]][[1]][1] 2 7 9
- Create a list
xin R such that:
x[[1]]returns the functionmeanx[[2]]returns the functionsdx[[3]][[1]]returns the vectorc(0, 1, 2)x[[3]][[2]]returns a function which calculates the cube root
Solution:
x <- list(mean, sd, list(c(0, 1, 2), function(y) y^(1/3)))