Chapter 7 Lists
Lists are a generic container that can hold objects of different classes. This makes lists quite handy to store heterogeneous data in R and are the preferred return type of many functions
List are created with the function list()
.
> x <- list(3.1416,"hello world", FALSE, c(1:10))
> x
[[1]]
[1] 3.1416
[[2]]
[1] "hello world"
[[3]]
[1] FALSE
[[4]]
[1] 1 2 3 4 5 6 7 8 9 10
The first element is a numeric vector, the second is a character vector, the third is a logical vector and the fourth an integer vector. As we can see, containing different classes is not a problem with lists.
However, the list is displayed a little bit different. It doesn’t print out like a vector because every element is different.