Setup
install.packages(c('dplyr', 'readr', 'tidyr'))
download.file("https://ndownloader.figshare.com/files/2292172",
"surveys.csv")
download.file("https://ndownloader.figshare.com/files/3299474",
"plots.csv")
download.file("https://ndownloader.figshare.com/files/3299483",
"species.csv")
Introduction
- Combine a series of data manipulation actions
- Do each action in sequential order
Intermediate variables
- Run a command
- Store the output in a variable
- Use that variable later in the code
-
Repeat
- Obtain the data for only DS, with no null weights, sorted by year, with only the year and weight columns
ds_data <- filter(surveys, species_id == "DS")
ds_data_no_null_weight <- drop_na(ds_data, weight)
ds_data_by_year <- arrange(ds_data_no_null_weight, year)
ds_weight_by_year <- select(ds_data_by_year, year, weight)
Pipes
- Intermediate variables can get cumbersome if there are lots of steps
|>(“pipe”) takes the output of one command and passes it as input to the next command- Want to take the mean of a vector
- Normally we would run the
meanfunction with the vector as the input:
x = c(1, 2, 3)
mean(x)
- Instead we could pipe the vector into the function
x |> mean()
- So
xbecomes the first argument inmean - If we want to add other arguments they get added to the function call
x = c(1, 2, 3, NA)
mean(x, na.rm = TRUE)
x |> mean(na.rm = TRUE)
- Questions?
surveys |>
filter(species_id == "DS")
ds_weight_by_year <- surveys |>
filter(species_id == "DS") |>
drop_na(weight)
ds_weight_by_year <- surveys |>
filter(species_id == "DS") |>
drop_na(weight) |>
arrange(year) |>
select(year, weight)
The magrittr pipe
- You will also see another type of pipe character
%>% - This is the original pipe in R and you had to load the magrittr package to use it (this gets loaded automatically by dplyr)
|>is now considered the standard unless you know you need special magrittr functionality
Keyboard Shortcut
- Shortcut: Ctrl-Shift-m