How can I stack columns per x columns in R

前端 未结 5 623
春和景丽
春和景丽 2021-01-07 14:25

I\'m looking to transform a data frame of 660 columns into 3 columns just by stacking them on each other per 3 columns without manually re-arranging (since I have 660 column

5条回答
  •  無奈伤痛
    2021-01-07 15:02

    reshape to the rescue:

    reshape(df, direction="long", varying=split(names(df), rep(seq_len(ncol(df)/2), 2)))
    
    #    time A  B id
    #1.1    1 1  4  1
    #2.1    1 2  5  2
    #3.1    1 3  6  3
    #1.2    2 7 10  1
    #2.2    2 8 11  2
    #3.2    2 9 12  3
    

提交回复
热议问题