0

In R, given the following data frame strucure:

Location   Date1   Date2   Date3
   a       value1  value2  value3
   b       value4  value5  value6
   c       value7  value8  value9

I would like to trasform the data frame as:

 Location  Values    Date
  a        value1   Date1
  a        value2   Date2
  a        value3   Date3
  b        value4   Date1
  b        value5   Date2
  ....

In order to collpse all columns into one I can use stack {utils}, but I cannot figure out how to assign the corresponding column name to each observation.

Caserio
  • 472
  • 1
  • 3
  • 14

1 Answers1

0

This is easily done using tidyr...

library(tidyr)
df2 <- df1 %>% gather(key=Date,value=Values,-Location)
Andrew Gustar
  • 17,295
  • 1
  • 22
  • 32