Hello,
I am looking at some of the pivot_longer
code at the moment and just want to understand: 1) how best to keep variables while making the data longer and 2) to understand how I can drop variables within the everything()
call and 3) some explanation on the arguments I can use for names_pattern
and what exactly (.)(.)
is doing?
library(tidyverse)
anscombe %>%
pivot_longer(everything(),
names_to = c(".value", "set"),
names_pattern = "(.)(.)"
)
#added in an ID variable I want to be retained to the corresponding rows and
#added in an additional set of columns to make longer too
anscombe_test <- cbind(id = c(letters[1:11]), anscombe, z1 = c(1:11),z2 = c(11:21), z3 = c(31:41), z4 = c(51:61))
anscombe_test %>%
pivot_longer(everything(),
names_to = c(".value", "set"),
names_pattern = "(.)(.)"
)