forked from swcarpentry/r-novice-gapminder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
13-dplyr.Rmd
194 lines (140 loc) · 7.93 KB
/
13-dplyr.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
---
layout: page
title: R for reproducible scientific analysis
subtitle: Dataframe manipulation with dplyr
minutes: 90
---
```{r, include=FALSE}
source("tools/chunk-options.R")
opts_chunk$set(fig.path = "fig/13-dplyr-")
# Silently load in the data so the rest of the lesson works
gapminder <- read.csv("data/gapminder-FiveYearData.csv", header=TRUE)
```
> ## Learning Objectives {.objectives}
>
> * To be able to use the 6 main dataframe manipulation 'verbs' with pipes in `dplyr`
>
Manipulation of dataframes means many things to many researchers, we often select certain observations (rows) or variables (columns), we often group the data by a certain variable(s), or we even calculate summary statistics. We can do these operations using the normal base R operations:
```{r}
mean(gapminder[gapminder$continent == "Africa", "gdpPercap"])
mean(gapminder[gapminder$continent == "Americas", "gdpPercap"])
mean(gapminder[gapminder$continent == "Asia", "gdpPercap"])
```
But this isn't very *nice* because there is a fair bit of repetition. Repeating yourself will cost you time, both now and later, and potentially introduce some nasty bugs.
## The `dplyr` package
Luckily, the [`dplyr`](https://cran.r-project.org/web/packages/dplyr/dplyr.pdf) package provides a number of very useful functions for manipulating dataframes in a way that will reduce the above repetition, reduce the probability of making errors, and probably even save you some typing. As an added bonus, you might even find the `dplyr` grammar easier to read.
Here we're going to cover 6 of the most commonly used functions as well as using pipes (`%>%`) to combine them.
1. `select()`
2. `filter()`
3. `group_by()`
4. `summarize()`
5. `mutate()`
If you have have not installed this package earlier, please do so:
```{r,eval=FALSE}
install.packages('dplyr')
```
Now let's load the package:
```{r,message=FALSE}
library(dplyr)
```
## Using select()
If, for example, we wanted to move forward with only a few of the variables in our dataframe we could use the `select()` function. This will keep only the variables you select.
```{r}
year_country_gdp <- select(gapminder,year,country,gdpPercap)
```
![](fig/13-dplyr-fig1.png)
If we open up `year_country_gdp` we'll see that it only contains the year, country and gdpPercap. Above we used 'normal' grammar, but the strengths of `dplyr` lie in combining several functions using pipes. Since the pipes grammar is unlike anything we've seen in R before, let's repeat what we've done above using pipes.
```{r}
year_country_gdp <- gapminder %>% select(year,country,gdpPercap)
```
To help you understand why we wrote that in that way, let's walk through it step by step. First we summon the gapminder dataframe and pass it on, using the pipe symbol `%>%`, to the next step, which is the `select()` function. In this case we don't specify which data object we use in the `select()` function since in gets that from the previous pipe. **Fun Fact**: There is a good chance you have encountered pipes before in the shell. In R, a pipe symbol is `%>%` while in the shell it is `|` but the concept is the same!
## Using filter()
If we now wanted to move forward with the above, but only with European countries, we can combine `select` and `filter`
```{r}
year_country_gdp_euro <- gapminder %>%
filter(continent=="Europe") %>%
select(year,country,gdpPercap)
```
> ## Challenge 1 {.challenge}
>
> Write a single command (which can span multiple lines and includes pipes) that will produce a dataframe that has the African values for `lifeExp`, `country` and `year`, but not for other Continents.
>How many rows does your dataframe have and why?
>
As with last time, first we pass the gapminder dataframe to the `filter()` function, then we pass the filtered version of the gapminder dataframe to the `select()` function. **Note:** The order of operations is very important in this case. If we used 'select' first, filter would not be able to find the variable continent since we would have removed it in the previous step.
## Using group_by() and summarize()
Now, we were supposed to be reducing the error prone repetitiveness of what can be done with base R, but up to now we haven't done that since we would have to repeat the above for each continent. Instead of `filter()`, which will only pass observations that meet your criteria (in the above: `continent=="Europe"`), we can use `group_by()`, which will essentially use every unique criteria that you could have used in filter.
```{r}
str(gapminder)
str(gapminder %>% group_by(continent))
```
You will notice that the structure of the dataframe where we used `group_by()` (`grouped_df`) is not the same as the original `gapminder` (`data.frame`). A `grouped_df` can be thought of as a `list` where each item in the `list`is a `data.frame` which contains only the rows that correspond to the a particular value `continent` (at least in the example above).
![](fig/13-dplyr-fig2.png)
## Using summarize()
The above was a bit on the uneventful side because `group_by()` much more exciting in conjunction with `summarize()`. This will allow use to create new variable(s) by using functions that repeat for each of the continent-specific data frames. That is to say, using the `group_by()` function, we split our original dataframe into multiple pieces, then we can run functions (e.g. `mean()` or `sd()`) within `summarize()`.
```{r}
gdp_bycontinents <- gapminder %>%
group_by(continent) %>%
summarize(mean_gdpPercap=mean(gdpPercap))
```
![](fig/13-dplyr-fig3.png)
That allowed us to calculate the mean gdpPercap for each continent, but it gets even better.
> ## Challenge 2 {.challenge}
>
> Calculate the average life expectancy per country. Which had the longest life expectancy and which had the shortest life expectancy?
>
The function `group_by()` allows us to group by multiple variables. Let's group by `year` and `continent`.
```{r}
gdp_bycontinents_byyear <- gapminder %>%
group_by(continent,year) %>%
summarize(mean_gdpPercap=mean(gdpPercap))
```
That is already quite powerful, but it gets even better! You're not limited to defining 1 new variable in `summarize()`.
```{r}
gdp_pop_bycontinents_byyear <- gapminder %>%
group_by(continent,year) %>%
summarize(mean_gdpPercap=mean(gdpPercap),
sd_gdpPercap=sd(gdpPercap),
mean_pop=mean(pop),
sd_pop=sd(pop))
```
## Using mutate()
We can also create new variables prior to (or even after) summarizing information using `mutate()`.
```{r}
gdp_pop_bycontinents_byyear <- gapminder %>%
mutate(gdp_billion=gdpPercap*pop/10^9) %>%
group_by(continent,year) %>%
summarize(mean_gdpPercap=mean(gdpPercap),
sd_gdpPercap=sd(gdpPercap),
mean_pop=mean(pop),
sd_pop=sd(pop),
mean_pop=mean(pop),
sd_pop=sd(pop))
```
> ## Advanced Challenge {.challenge}
> Calculate the average life expectancy in 2002 of 2 randomly selected countries for each continent. Then arrange the continent names in reverse order.
> **Hint:** Use the `dplyr` functions `arrange()` and `sample_n()`, they have similar syntax to other dplyr functions.
>
> ## Solution to Challenge 1 {.challenge}
>```{r}
>year_country_lifeExp_Africa <- gapminder %>%
> filter(continent=="Africa") %>%
> select(year,country,lifeExp)
> ```
> ## Solution to Challenge 2 {.challenge}
>```{r}
>lifeExp_bycountry <- gapminder %>%
> group_by(country) %>%
> summarize(mean_lifeExp=mean(lifeExp))
>```
> ## Solution to Advanced Challenge {.challenge}
>```{r}
>lifeExp_2countries_bycontinents <- gapminder %>%
> filter(year==2002) %>%
> group_by(continent) %>%
> sample_n(2) %>%
> summarize(mean_lifeExp=mean(lifeExp)) %>%
> arrange(desc(mean_lifeExp))
>```
## Other great resources
[Data Wrangling Cheat sheet](https://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf)
[Introduction to dplyr](https://cran.rstudio.com/web/packages/dplyr/vignettes/introduction.html)