List structure
Often when working in R, you'll use data frames or vectors. Another kind of R object is a list. While lists can be complicated, lists are also incredibly powerful. Lists are like Hermione Granger's bag of holding (from Harry Potter); they can hold a wide variety of things. The contents of a list don't have to be the same data type, and as long as you know how it's organized, you can grab out what you need by subsetting.
Both named and unnamed lists can be subset using double square brackets [[ ]]
list this: listname[[ index ]]
If a list is named, you can also use $
for subsetting. The syntax list$elementname
pulls out the named element from the list. Like any other kind of object in R, you can use the str()
to determine the structure of the list.
This exercise is part of the course
Foundations of Functional Programming with purrr
Exercise instructions
- Load the
repurrrsive
package. - Load the
wesanderson
dataset. - Examine the structure of the first element in
wesanderson
. - Examine the structure of the
GrandBudapest
element inwesanderson
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Load repurrrsive package, to get access to the wesanderson dataset
library(___)
# Load wesanderson dataset
data(___)
# Get structure of first element in wesanderson
str(___)
# Get structure of GrandBudapest element in wesanderson
str(___$___)