-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlists.Rmd
65 lines (40 loc) · 1.51 KB
/
lists.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
---
title: "Lists"
description: |
Lists are our friend!
output: distill::distill_article
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Lists can not only store a mix of data types, but also more complex data (e.g. data frames, even lists themselves!). It's an ideal option for a group of similar complex data, and decently sets us up for iteration! So in preparation for `for` loops, let's examine lists and how to extract data from them.
# Create a list
```{r eval=FALSE}
# an empty list
l <- list()
# populated with objects
l <- list(file_01, file_02, file_03)
```
# Anatomy
There's several layers to a list, like a container within a container. To extract a specific element's data, use double brackets instead one.
```{r eval=FALSE}
l # the whole list and all its elements
l[1] # the first element in its container; contains the name/index of element and the data
l[[1]] # the data of the first element
```
Hadley Wickham's pepper analogy

## Names
Like vectors, lists can also be named
```{r eval=FALSE}
names(l) <- c('table9', 'table10', 'table11')
```
Now you can also access the data of a specific element by name using $ or [[]]
```{r eval=FALSE}
l$table9
l[['table9']]
# saving processes back into the list
l$table9 <- l$table9 %>% filter(cnty == '033')
```
Now if you extract an element with only one `[]` like `l[1]`, you can see that it's a container holding the name of the element and the data itself.
