将一个列数据帧重新整形为R中的两列

时间:2016-03-02 18:45:45

标签: r

让我说这样的数据框(df)带有字符

head
---
a
b
c
d
e
f

我想重塑df为

head1   head2
-----   -----
a        b
c        d
e        f

如何在不使用任何循环的情况下进行此类重塑。我会很高兴得到任何帮助。非常感谢。

1 个答案:

答案 0 :(得分:3)

我们可以转置(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
相关问题