I have a data frame which each row indicates a unique id.
ID <- 1:12
Date1 <- seq(as.Date("2000/1/1"), length.out = 12, by = "months")
Date2 <- seq(as.Date("2001/1/1"), length.out = 12, by = "months")
Date3 <- seq(as.Date("2002/1/1"), length.out = 12, by = "months")
Fcast1 <- rnorm(12)
Fcast2 <- rnorm(12)
Fcast3 <- rnorm(12)
df <- data.frame(ID, Date1, Fcast1, Date2, Fcast2, Date3, Fcast3)
I would like to gather Date1 to Date3 and Fcast1 to Fcast3 columns in two columns of Date and Fcast and repeat IDs 3 times. basically creating long-view of data or rbind-ing each pair of Date and Fcast. Desired Output shape:
ID <- rep(ID, 3)
Date = c(Date1, Date2, Date3)
Fcast = c(Fcast1, Fcast2, Fcast3)
df <- data.frame(ID, Date, Fcast)