Application of pipe (%>%) functions
Useful tricks and features in OMICs mining


PreviousUseful R tricks and features in OMICs miningNextChanging data frames format with pivot_longer()
Last updated
Useful tricks and features in OMICs mining


Last updated
# Using pipes for the first time
# Step 1: call library
library(tidyverse)
# First exemplary pipe (short pipe - command in one line)
# Arranging CE 16:1 concentrations in descending order (from high to low)
data %>% arrange(desc(`CE 16:1`))
# Explanation of the pipe:
# 1) Take object 'data' from the global environment,
# 2) Push it through the pipe (from the left to the right),
# 3) Pull the column `CE 16:1`,
# 4) Arrange concentrations from high to low (descending order).
# Alternatively, you could use:
data %>%
arrange(desc(`CE 16:1`))
# or without pipe (pipe is not necessary for short operations)
arrange(data, desc(`CE 16:1`))# Long pipeline - style guide for pipes (PIPELINE 1)
data %>%
select(`Label`, `SM 39:1;O2`) %>%
filter(Label == 'N' | Label == 'T') %>%
group_by(Label) %>%
summarize_if(is.numeric, mean)
# Explanation:
# 1) Take 'data' tibble from the global environment,
# 2) Push through the pipe,
# 3) Select columns: `Label` & `SM 39:1;O2` from 'data',
# 4) Push through the pipe,
# 5) Filter entries using `Label` column: keep entries containing `Label` 'N' OR 'T',
# 6) Push through the pipe,
# 7) Group by `Label`,
# 8) Push through the pipe,
# 9) Use 'summarize' if a column is numeric - calculate the mean for it.
# Pipeline with functions containing long arguments: select, pivot_longer, summarise;
# (PIPELINE 2)
data %>%
select(`Sample Name`,
`Label`,
`LPC 16:0`,
`LPC 18:0`,
`LPC 18:1`,
`LPC 18:2`,
`SM 32:1;O2`,
`SM 39:1;O2`,
`SM 41:1;O2`) %>%
pivot_longer(cols = `LPC 16:0`:`SM 41:1;O2`,
names_to = 'Lipids',
values_to = 'Concentration') %>%
filter(Label == 'N' | Label == 'T') %>%
group_by(Label, Lipids) %>%
summarise(
`Mean concentration` = mean(Concentration),
`SD concentration` = sd(Concentration))
# Explanation:
# 1) Take 'data' tibble from the global environment,
# 2) Push through the pipe,
# 3) Select columns,
# 4) Change the wide data frame into a long data frame,
# 5) Filter entries using `Label` column: keep 'N' OR 'T' entries,
# 6) Push through the pipe,
# 7) Group entries by `Label` and then `Lipids` (long table!),
# 8) Summarize the long table grouping data by `Label` & `Lipids`: calculate mean & sd# Three ways of storing the output of pipelines (assignment):
# 1) At the beginning of the code, in one line with the pipeline,
# as 'mean.value' object, with <-:
mean.value <- data %>%
select(`Label`, `SM 39:1;O2`) %>%
filter(Label == 'N' | Label == 'T') %>%
group_by(Label) %>%
summarize_if(is.numeric, mean)
# 2) At the beginning of the code, in two separate lines,
# as 'mean.value' object, with <-:
mean.value <-
data %>%
select(`Label`, `SM 39:1;O2`) %>%
filter(Label == 'N' | Label == 'T') %>%
group_by(Label) %>%
summarize_if(is.numeric, mean)
# 3) At the end of the code, as 'mean.value' object, with ->:
data %>%
select(`Label`, `SM 39:1;O2`) %>%
filter(Label == 'N' | Label == 'T') %>%
group_by(Label) %>%
summarize_if(is.numeric, mean) ->
mean.value