(Sections 9.1-9.2)
Lists: a new data structure in R.
Lists are non-atomic vectors. (Their elements don’t all have to be the same type of data.)
Example:
lst1
You can make an empty list, too:
If a list element has a name, then you can get to it with the $
:
You can get to elements of a list inside a list in the same way:
$nums
[1] 10 11 12 13 14 15 16 17 18 19 20
$bools
[1] TRUE FALSE FALSE
$george
$george$name
[1] "Dorothy"
$george$age
[1] 12
This works with the [
-operator (just as with vectors). The result is another list, as with vectors.
Sub-setting a list yields another list, even when your numerical “location” vector has only one element.
Two ways:
$
if the element has a name:[[
-operator always works.This “flattens” the list into a vector:
If elements are of different data types then they are coerced to be of the same type.