如何在 R 中按 x 列堆叠列 [英] How can I stack columns per x columns in R
本文介绍了如何在 R 中按 x 列堆叠列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望将 660 列的数据框转换为 3 列,只需将它们每 3 列堆叠在一起,而无需手动重新排列(因为我有 660 列).
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).
在每 2 列只有 4 列的小规模示例中,我想从
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
到
A B
1 4
2 5
3 6
7 10
8 11
9 12
谢谢
推荐答案
reshape
来拯救:
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
这篇关于如何在 R 中按 x 列堆叠列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文