You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/class_worksheets/09_apply_lists/09_apply_list.qmd
+30-23Lines changed: 30 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -19,94 +19,101 @@ answers = FALSE
19
19
20
20
## Overview
21
21
22
-
The apply family of functions can be a handy way to perform analyses and transformations of data quickly. Today we will be using both to improve some previous tasks. First we will be revisiting `comma_split()` (for the last time), and solving an old annoyance regarding reading in several data files.
22
+
The apply family of functions can be a handy way to perform repetitive tasks quickly. Today we will be using apply functions to improve some previous tasks. First we will be revisiting `comma_split()` (for the last time) and learning to use it on several columns at once. Next, we will be tackling a common annoyance when it comes to loading several files into R.
23
23
24
24
## The Data
25
25
26
26
We are going to be using class survey data for lab today. Please load it in using the following:
The first thing we will be doing today is combining our `pet_split()` function and `lapply()` in order to split all of our `<DRINK>_day` comma separated columns at once. I've provided our fully generalized version of `pet_split()` below. Run the following to add it to your environment.
34
+
The first thing we will be doing today is combining our `comma_split()` function and `lapply()` in order to split all of our `<DRINK>_day` comma separated columns at once. I've provided the fully generalized version of `comma_split()` we've developed in the past few worksheets below. Run the following to add it to your environment.
pet_output[pet_output$other == "" & !is.na(pet_output$other), 'other'] = NA
75
+
output[output$other == "" & !is.na(output$other), 'other'] = NA
76
76
77
77
# return output
78
-
return(pet_output)
78
+
return(output)
79
79
}
80
80
```
81
81
82
-
Once you've got the function in your environment, we are going to use `lapply()` to apply it over all the relevant columns in our `survey` dataframe.
82
+
Once you've got the function in your environment, we are going to use `lapply()` to apply it over all the relevant columns in our `survey` dataframe. The first argument to `lapply()` will be the thing you want to apply over. In this case it will be the `survey` dataframe. The second argument, `FUN`, is the function you want to apply to the first argument, `comma_split()`.
83
+
84
+
{{% notice tip %}}
85
+
Note that when supplying a dataframe to `lapply()` it interprets this as applying `FUN` to each column in the dataframe.
86
+
{{% /notice %}}
87
+
88
+
This covers what we want to apply (`FUN`) and what we want to apply it to (`survey`), but there is one hangup. How do we pass our arguments to `comma_split()`? The first argument of `FUN` is always assumed to be the current part of your data the apply function is going over. In the case of `survey` and `comma_split()` the first argument to `comma_split()`, `vector_to_split`, will be the columns from `survey`.
89
+
90
+
But how do we specify the rest of the arguments? This is where some of our advanced function knowledge comes in. The third argument to `lapply()` is `...`, which you may recall means we can *pass* arguments through the `lapply()` function. As long as we put in an argument to `lapply()` that matches the argument names in `FUN`, (`comma_split()`), they will be passed through. So in this case if we supply `lapply()` with an argument called `possible_columns`, that will be passed to `comma_split()`.
83
91
84
92
::: {.question}
85
-
Use `lapply()` to apply `pet_split()` to all of the `<DRINK>_days` columns in our `survey` dataframe. Save the results as `drink_dfs`. **DO NOT** include the `()` after `pet_split` when providing it as an argument; it will produce an error.
93
+
Use `lapply()` to apply `comma_split()` to all of the `<DRINK>_days` columns in our `survey` dataframe. Save the results as `drink_dfs`. **DO NOT** include the `()` after `comma_split` when providing it as an argument; it will produce an error.
You will have gotten a list object of length 5 back. Recall that lists are super-vectors. In this case, each element of our list contains an entire dataframe!
99
-
100
-
101
-
106
+
You will have gotten a list object of length 5 back. Recall that lists are super-vectors. In this case, each element of our list contains an entire dataframe! Each of these dataframes is the normal output from `comma_split()`. We could combine these with our `survey` data if we cared to dome some analyses.
102
107
108
+
## Read ALL the Files!
103
109
110
+
Using `lapply()` does not need to be limited to uses within R. One common way to use it is when loading in data. To try this, we're going to load in a number of `.csv` files containing economic and population data on Massachusetts from the [American Community Survey five-year estimates (ACS5)](https://www.census.gov/data/developers/data-sets/acs-5year.html). You can think of it as a yearly mini-census.
104
111
112
+
First, we need to have a directory of data files with identical structures. For this example, we can use some data I have hosted on GitHub.
105
113
106
114
107
-
## Read ALL the Files!
108
115
109
-
I promised I would share how to read multiple files at once and here we are. This is actually a common way I use `lapply()`. First, we need to have a directory of data files with identical structures. For this example, we will use the data from lab 3 (aggregation and merging). First, specify the path to the data folder of lab 3. You have to find the specific path on your computer.
116
+
we will use the data from lab 3 (aggregation and merging). First, specify the path to the data folder of lab 3. You have to find the specific path on your computer.
0 commit comments