13.5 Recursive vectors (lists)
- Lists can contain other lists implying that are more complex than atomic vectors.
- Hence, suitable to represent hierarchical or tree-like structures.
- Use
list()
to create lists.
- We use the function
str()
to assess the structure.
- Lists can be named:
list()
can contain a mix of objects unlike atomic vectors.
- Lists can contain other lists!
13.5.1 Visualising lists
- Let’s see a visual representation of lists. E.g.,
A visual drawing of the lists:
There are three principles:
Lists have rounded corners. Atomic vectors have square corners.
Children are drawn inside their parent, and have a slightly darker background to make it easier to see the hierarchy.
The orientation of the children (i.e. rows or columns) isn’t important, a row or column orientation are picked to either save space or illustrate an important property in the example. ??
13.5.2 Subsetting
- We can subset lists using three ways, let’s see the example below.
[
extracts a sub-list. And the result will always be a list.
We can also subset lists with a logical, integer or character vector as seen in vectors.
[[
extracts a single component from a list. It removes a level of hierarchy from the list.
$
is a shorthand to extract named elements of a list. It works similarly to[[
except that you don’t need to use quotes.
- Difference between
[
and[[
is important for lists:[[
drills down into the list while[
returns a new, smaller list. Let’s compare the code and output above with the visual shown below.
13.5.3 Lists of condiments
- Let’s discuss an illustration of the difference between
[
and[[
to solidify our understanding! :) - Below we see an usual pepper shaker, and let it be our list
x
.
x[1]
is a pepper shaker containing a single pepper packet:
x[2]
would look the same, but would contain the second packet. x[1:2]
would be a pepper shaker containing two pepper packets.
x[[1]]
is:
x[[1]][[1]]
to get the content of the pepper package:
13.5.4 Exercises
- Draw the following lists as nested sets:
To draw pretty a visual diagram, refer to DiagramR R Package to render Graphviz diagrams.
But what I have is hand-drawn. Not pretty!! :)
For a:
For b:
2. What happens if you subset a tibble as if you’re subsetting a list? What are the key differences between a list and a tibble?
Subsetting a tibble works the same way as a list; a data frame can be thought of as a list of columns.
The key difference between a list and a tibble -> all the elements (columns) of a tibble must have the same length (number of rows). Lists can have vectors with different lengths as elements.