0

I have a Multi-dimensional data.frame like this:
df1

   site   date   2019-01-01   2019-01-01 2019-01-01 2019-01-02  2019-01-02 2019-01-02 2019-01-03 ...    
          types     PM2.5        O3         NO2        PM2.5       O3          NO2       PM2.5
#1 1001A              1           1          1           1          1           1          1 
#2 1002A              1           1          1           1          1           1          1 
#3 1003A              1           1          1           1          1           1          1 
    ...

My desired output would be:

      site   date        type   value
#1   1001A   2019-01-01  PM2.5   1
#2   1001A   2019-01-01   O3     1
#3   1001A   2019-01-01   NO2    1
#4   1001A   2019-01-02  PM2.5   1      
#5   1001A   2019-01-02   O3     1
#6   1001A   2019-01-02   NO2    1
             ....
#n+1 1002A   2019-01-01  PM2.5   1
#n+2 1002A   2019-01-01   O3     1
#n+3 1002A   2019-01-01   NO2    1
#n+4 1002A   2019-01-02  PM2.5   1      
#n+5 1002A   2019-01-02   O3     1
#n+6 1002A   2019-01-02   NO2    1
             ....

How should I deal with it
Many thanks!!!

jinchi
  • 41
  • 3
  • I'm too busy today to write a full answer, but look into `gather` and `spread`, the documentation is very good. – mmyoung77 Aug 19 '20 at 14:39
  • @mmyoung77 OK, thank you a lot! – jinchi Aug 19 '20 at 14:41
  • `gather` and `spread` have been superseded by `pivot_longer` and `pivot_wider`. Does this answer your question? Some of those answers are out of date though. I would look at the "tidyr 1.0.0" or "data.table" solutions, personally. https://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format?rq=1 –  Aug 19 '20 at 14:41
  • I'm a bit confused by the second line in your example. Could you please `dput(head(df1,5))`. is the second line a row in the dataframe? – Chuck P Aug 19 '20 at 15:15

0 Answers0