How can I stack columns per x columns in R
问题 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 columns). In a small scale example per 2 columns with just 4 columns, I want to go from A B C D 1 4 7 10 2 5 8 11 3 6 9 12 to A B 1 4 2 5 3 6 7 10 8 11 9 12 Thanks 回答1: 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