The preferred method when using dplyr and other tidyverse package functions is to use pipes.
The tidyverse pipe symbol is |> (historically it was %>%).
Pipes allow you to more easily combine multiple functions together with a logical flow. A big advantage of pipes is that they are generally easier to read compared to nesting functions within each other.
This page will give a brief introduction to pipes using various functions that are covered in other pages of this section. Other pages will demonstrate further examples of pipes.
Dataset
For demonstration we’ll load the hbr_maples data from the lterdatasampler package (hyperlink includes install instructions).
Note: When piping, the first variable of the post pipe function is the piped data/object.
#Load packagelibrary("lterdatasampler")#hbr_maples tibble for demonstrationmaples_tbl <- tibble::as_tibble(lterdatasampler::hbr_maples)maples_tbl
Note: Pipes are not only limited to tidyverse functions.
maples_tbl |>#Extract indexes 1 to 1000 dplyr::slice(1:1000) |>#Extract only low elevation samples dplyr::filter(elevation =="Low") |>#Select the columns leaf1area and leaf2area dplyr::select(c(leaf1area,leaf2area)) |>#Summarise tibblesummary()
leaf1area leaf2area
Min. : 2.480 Min. : 3.444
1st Qu.: 9.308 1st Qu.: 9.548
Median :12.110 Median :11.891
Mean :12.239 Mean :12.305
3rd Qu.:15.405 3rd Qu.:15.588
Max. :26.198 Max. :24.235
leaf1area leaf2area
Min. : 2.480 Min. : 3.444
1st Qu.: 9.308 1st Qu.: 9.548
Median :12.110 Median :11.891
Mean :12.239 Mean :12.305
3rd Qu.:15.405 3rd Qu.:15.588
Max. :26.198 Max. :24.235