0

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)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213

2 Answers2

0

You can do something like:

library(data.table)
setDT(df)
melt(df, measure.vars=patterns("^Date", "^Fcast"), value.name=c("Date", "Fcast"))[, 
    variable := NULL][]
chinsoon12
  • 25,005
  • 4
  • 25
  • 35
0

Using tidyr::pivot_longer -

tidyr::pivot_longer(df, 
                    cols = -ID, 
                    names_to = '.value', 
                    names_pattern = '(.*)\\d+')

# A tibble: 36 x 3
#      ID Date        Fcast
#   <int> <date>      <dbl>
# 1     1 2000-01-01  0.452
# 2     1 2001-01-01  0.242
# 3     1 2002-01-01 -0.540
# 4     2 2000-02-01  1.54 
# 5     2 2001-02-01  0.178
# 6     2 2002-02-01  0.883
# 7     3 2000-03-01 -0.987
# 8     3 2001-03-01  1.40 
# 9     3 2002-03-01  0.675
#10     4 2000-04-01 -0.632
# … with 26 more rows
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213