Suppose I have the following df
original = data.frame(ID= c(1,1, 2),
A = c(1,NA,1),
StartingA = c("2001-01-01", NA, "1999-03-03"),
EndingA = c("2002-01-01", NA, "2000-03-03"),
B = c(NA,1,1),
StartingB = c(NA, "2016-01-01", "2004-03-17"),
EndingB = c(NA, "2019-01-01", "2018-11-27"),
C = c(1,NA,1),
StartingC = c("2011-07-08", NA, "2019-01-01"),
EndingC = c("2017-07-08", NA, "2019-05-01"))
I want to pivot from wide to long and to get as result:
result = data.frame(ID = c(1, 1, 1, 2, 2, 2),
Value = c("A", "C", "B", "A", "B", "C"),
Starting = c("2001-01-01", "2011-07-08", "2016-01-01", "1999-03-03", "2004-03-17", "2019-01-01"),
EndingA = c("2002-01-01", "2017-07-08", "2019-01-01", "2000-03-03", "2018-11-27", "2019-05-01"))
I have more than 40 columns like these ones.
My attempts with pivot_longer were not correct