Changing data frames format with pivot_longer()
Useful tricks and features in OMICs mining



Last updated
Useful tricks and features in OMICs mining



Last updated
# Call tidyverse collection
library(tidyverse)
# We will use pipes for preparing long data frame
# The new long data frame will be stored in 'data.long'
# The following pipeline will be used:
data.long <-
data %>%
pivot_longer()
# 1. Take wide 'data' from the global environment
# 2. Push it through the pipe
# 3. Change wide data into long using pivot_longer()# Opening help (vignette) regarding pivot_longer()
?pivot_longer()# Specifying arguments of pivot_longer() function:
# Option no. 1: indicate the range of columns by name (in our case - all lipids)
data.long.no.1 <-
data %>%
pivot_longer(cols = `CE 16:1` : `SM 42:1;O2`,
names_to = 'Lipids',
values_to = 'Concentration')
# The new data frame is stored in the global environment as 'data.long.no.1'
# Option no. 2: indicate the range of columns by their number:
data.long.no.2 <-
data %>%
pivot_longer(cols = 3:129,
names_to = 'Lipids',
values_to = 'Concentration')
# The new data frame is stored in the global environment as 'data.long.no.2'
# Option no. 3: indicate the range of columns by their type (take all numeric):
data.long.no.3 <-
data %>%
pivot_longer(cols = where(is.numeric),
names_to = 'Lipids',
values_to = 'Concentration')
# The new data frame is stored in the global environment as 'data.long.no.3'# Checking the type of the new object
is_tibble(data.long.no.1)
# or
print(data.long.no.1)# Glimpse at the new object
glimpse(data.long.no.1)# Recreating wide data tibble:
data.wide <- data.long.no.3 %>%
pivot_wider(names_from = Lipids,
values_from = Concentration)