Basic operator chaining using pipes
Introduction
We have already covered basic operations in tidyverse:
- filter rows
- mutate or add new columns using existing data
- group by and summarize
- rearrange columns
- rename columns
- select columns
- sort rows
- append new column to existing dataframe
- append new row to existing dataframe
We can combine these operations in a chain with the help of the Pipe operation %>%. The pipe operation has takes an input does the processing and gives an output.
The pipe operation does not modify the current input, instead it returns a new dataframe which is a result of the chained operation performed.
Procedure
We will be working with a custom dataframe:
# package for creating dataframe
library(tibble)
# tibble or dataframe
df <- tibble(col1 = as.integer(c(1,2,3,4,5)),
col2 = c(11,12,13,14,15),
)
View(df)
We will be applying the following operation on the dataframe:
- add new column col3 with values 10, 20, 30, 40, 50, 60
- rename the old column col1 to renamed_col1
- filter to keep rows which have even values for renamed_col1
- rearrange columns in dataframe to following order renamed_col1, col3, col2
Code
# refer procedure for definition of df
library(dplyr)
result <- df %>%
# add new column col3
mutate(col3 = c(10,20,30,40,50)) %>%
# rename old column col1 to renamed_col1
rename(renamed_col1 = col1) %>%
# filter to keep rows which have even values for renamed_col1
filter(renamed_col1 %% 2 == 0) %>%
# rearrange columns to order renamed_col1, col3, col2
select(renamed_col1, col3, col2)
View(result)
The output of above code is:
Conclusion
Thus we have successfully implemented basic chain operation using pipes.
References
- https://r4ds.had.co.nz/