# Data Manipulation using dplyr

Bracket subsetting is handy, but it can be cumbersome and difficult to read, especially for complicated operations. Enter dplyr. dplyr is a package for making data manipulation easier.

Packages in R are basically sets of additional functions that let you do more stuff. The functions we’ve been using so far, like str() or data.frame(), come built into R; packages give you access to more of them. Before you use a package for the first time you need to install it on your machine, and then you should to import it in every subsequent R session when you’ll need it.

install.packages("dplyr")

While we’re installing stuff, let’s also install the ggplot2 package, which we’ll use next.

install.packages("ggplot2")

You might get asked to choose a CRAN mirror – this is basically asking you to choose a site to download the package from. The choice doesn’t matter too much; we recommend the RStudio mirror.

library(dplyr)    ## load the package

## What is dplyr?

The package dplyr provides easy tools for the most common data manipulation tasks. It is built to work directly with data frames. The thinking behind it was largely inspired by the package plyr which has been in use for some time but suffered from being slow in some cases.dplyr addresses this by porting much of the computation to C++. An additional feature is the ability to work with data stored directly in an external database. The benefits of doing this are that the data can be managed natively in a relational database, queries can be conducted on that database, and only the results of the query returned.

This addresses a common problem with R in that all operations are conducted in memory and thus the amount of data you can work with is limited by available memory. The database connections essentially remove that limitation in that you can have a database of many 100s GB, conduct queries on it directly, and pull back just what you need for analysis in R.

### Selecting columns and filtering rows

We’re going to learn some of the most common dplyr functions: select(), filter(), mutate(), arrange(), group_by(), and summarize(). To select columns of a data frame, use select(). The first argument to this function is the data frame (surveys), and the subsequent arguments are the columns to keep.

selected_col <- select(surveys, species_id, year, weight)
head(selected_col)

To choose rows, use filter():

surveys1995 <- filter(selected_col, year == 1995)
head(surveys1995)

### Pipes

The pipe operator (%>%) from the magrittr package makes it easy to chain these actions together: the output of one function becomes the input of the next.

surveys %>%
filter(year==1995) %>%
select(species_id, year, weight)

Another cumbersome bit of typing. In RStudio, type Ctrl + Shift + M and the %>% operator will be inserted.

In the above we use the pipe to send the surveys data set first through filter, to keep rows where year was 19955, and then through select to keep the species_id, year, and weight columns. When the data frame is being passed to the filter() and select() functions through a pipe, we don’t need to include it as an argument to these functions anymore.

If we wanted to create a new object with this smaller version of the data we could do so by assigning it a new name:

surveys1995 <- surveys %>%
filter(year==1995) %>%
select(species_id, year, weight)

Note that the final data frame is the leftmost part of this expression.

### Challenge

Using pipes, subset the data to include individuals with weight less than 5, and retain the columns year, sex, and weight.

### Mutate

Frequently you’ll want to create new columns based on the values in existing columns, for example to do unit conversions, or find the ratio of values in two columns. For this we’ll use mutate().

To create a new column of weight in kg:

surveys %>%
mutate(weight_kg = weight / 1000)

If this runs off your screen and you just want to see the first few rows, you can use a pipe to view the head() of the data (pipes work with non-dplyr functions too, as long as the dplyr or magrittr packages are loaded).

surveys %>%
mutate(weight_kg = weight / 1000) %>%
head

### Challenge

Create a new dataframe from the survey data that

• contains only the species_id column and a column with the square-root of hindfoot_length values (e.g. a new column hindfoot_sqrt).

• also, for the hindfoot_sqrt column, all values are < 3.

### Split-apply-combine data analysis and the summarize() function

Many data analysis tasks can be approached using the “split-apply-combine” paradigm: split the data into groups, apply some analysis to each group, and then combine the results. dplyr makes this very easy through the use of the group_by() function. group_by() splits the data into groups upon which some operations can be run. For example, if we wanted to group by sex and find the number of rows of data for each sex, we would do:

surveys %>%
group_by(sex) %>%
tally()

Here, tally() is the action applied to the groups created to group_by() and counts the total number of records for each category. group_by() is often used together with summarize() which collapses each group into a single-row summary of that group. So to view mean weight by sex:

surveys %>%
group_by(sex) %>%
summarize(mean_weight = mean(weight))

Note that tally() was a shortcut to using summarize() with the n() function:

surveys %>%
group_by(sex) %>%
summarize(n = n())

You can group by multiple columns too:

surveys %>%
group_by(sex, species_id) %>%
summarize(mean_weight = mean(weight))

We might also sort the rows by mean_weight, using arrange().

surveys %>%
group_by(sex, species_id) %>%
summarize(mean_weight = mean(weight)) %>%
arrange(mean_weight)

If you want them sorted from highest to lowest, use desc().

surveys %>%
group_by(sex, species_id) %>%
summarize(mean_weight = mean(weight)) %>%
arrange(desc(mean_weight))

Also note that you can include multiple summaries.

surveys %>%
group_by(sex, species_id) %>%
summarize(mean_weight = mean(weight),
min_weight = min(weight)) %>%
arrange(desc(mean_weight))

### Challenge

How many times was each plot_type surveyed?

### Challenge

Use group_by() and summarize() to find the min and max hindfoot length for each species.

In preparation for the data visualization session after lunch, I want you to prepare three data sets:

### Challenge

Prepare three data sets for the data visualization session:

1. just_dm containing only the observations from with species_ID == "DM"

2. stat_summary containing the average weight and hindfoot length for each species, as well as a count of the number of observations for each species (using the n() function).

3. year_summary containing the yearly average weight, hindfoot length, and count data for each species and sex combination. (Hint, you only need to change the group_by() part from #2 to do this.)