假设我有这样一个带有字符的数据框(df)
head
---
a
b
c
d
e
f
我想将 df reshape 为
head1 head2
----- -----
a b
c d
e f
如何在不使用任何循环的情况下进行这样的 reshape 。我会很高兴任何帮助。非常感谢。
最佳答案
我们可以转置 ( t
) 数据集(即 matrix
),然后调用 matrix
与 ncol
, 并将其重新转换为 data.frame
as.data.frame(matrix(t(df1), ncol=2,
byrow=TRUE), stringsAsFactors=FALSE)
# V1 V2
#1 a b
#2 c d
#3 e f
关于将一列数据框 reshape 为 R 中的两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35755586/