I am trying to transform two sequences columns of my dataset from wide to long. I tried following this thread, but have not been able to follow it : Using pivot_longer with 2 groups of columns
Here is an example data set (sorry - not that great at generating sample data yet):
df <- data.frame(id = c(1,2,3,4,5,6,7),
wt = c(12,14,16,17,17,15,14),
gen = c("m", "f","m", "f","m","m", "f"),
start_date_1 = c("2022-04-03" ,"2022-04-03", "2022-04-03", "2022-04-03", "2022-04-03", "2022-04-03", "2022-04-03"),
start_date_2 = c("2022-04-03" ,"2022-04-03", "2022-04-03", "2022-04-03", "2022-04-03", "2022-04-03", "2022-04-03"),
start_date_3 = c("2022-04-03" ,"2022-04-03", "2022-04-03", "2022-04-03", "2022-04-03", "2022-04-03", "2022-04-03"),
end_date_1 = c("2022-04-03" ,"2022-04-03", "2022-04-03", "2022-04-03", "2022-04-03", "2022-04-03", "2022-04-03"),
end_date_2 = c("2022-04-03" ,"2022-04-03", "2022-04-03", "2022-04-03", "2022-04-03", "2022-04-03", "2022-04-03"),
end_date_3 = c("2022-04-03" ,"2022-04-03", "2022-04-03", "2022-04-03", "2022-04-03", "2022-04-03", "2022-04-03")
)
and here is the code I am trying:
test_pivot<- df %>%
pivot_longer(cols = everything(),
names_to = c(".value", "end_date"),
names_pattern = "([a-z]+_)(\\d)")
The output I am hoping to get is:
ID start_date start_date_date end_date end_date_date
1 start_date_1 2022-04-03 end_date_1 2022-05-07
1 start_date_2 2022-04-08 end_date_2 2022-05-06
1 start_date_3 2022-04-22 end_date_3 2022-05-17
1 start_date_4 2022-04-01 end_date_4 2022-05-18
Within this dataset there are 100s of columns and 1000s of unique IDs. In the example dataset, the columns are consecutive, but in the actual dataset the columns could come in any order.
Thanks of your help