简体   繁体   English

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

[英]Reshaping one column data frame into two columns in R

let say I such a data frame(df) with chracters 让我说这样的数据框(df)与chracters

head
---
a
b
c
d
e
f

I want to reshape df as 我想重塑df as

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

How can I do such a reshape without using any loop. 如何在不使用任何循环的情况下进行这样的重塑。 I will be very glad for any help. 我会很高兴得到任何帮助。 Thanks a lot. 非常感谢。

We can transpose ( t ) the dataset (which will be matrix ), then call the matrix with the ncol , and reconvert it to data.frame 我们可以调换( t )的数据集(这将是matrix ),然后调用matrixncol ,并重新转换它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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM