Convert data from long format to wide format with multiple measure columns

前端 未结 5 2057
灰色年华
灰色年华 2020-11-22 02:27

I am having trouble figuring out the most elegant and flexible way to switch data from long format to wide format when I have more than one measure variable I want to bring

5条回答
  •  逝去的感伤
    2020-11-22 03:23

    Using the data.table_1.9.5, this can be done without the melt as it can handle multiple value.var columns. You can install it from here

     library(data.table)
     dcast(setDT(my.df), ID~TIME, value.var=c('X', 'Y'))
     #   ID 1_X 2_X 3_X 4_X 5_X 1_Y 2_Y 3_Y 4_Y 5_Y
     #1:  A   1   4   7  10  13  16  19  22  25  28
     #2:  B   2   5   8  11  14  17  20  23  26  29
     #3:  C   3   6   9  12  15  18  21  24  27  30
    

提交回复
热议问题