I need to convert a dataframe to longer form. for example, my dataframe will be like
df <- data.frame(
group = c("group1","group2"),
x1 = c(3,4),
x2 = c(5,6),
y1 = c(7,8),
y2 = c(9,10)
)
I need to convert it to
df2 <- data.frame(
group = c("group1","group1","group2","group2"),
num = c(1,2,1,2),
x = c(3,5,4,6),
y = c(7,9,8,10)
)
I want to use tidyr::pivot_longer but couldn't figure out names_pattern here. Am I on the right track?
df_2<-df%>%tidyr::pivot_longer(c("x1","x2","y1","y2"),
names_to = c("x","y"),
names_pattern = "")
Any help? Thanks.