简体   繁体   English

如何将一列数据重组为多列数据

[英]How to reorganize one column data to multi-column data

I have a dataset read from xls looks like this: 我有一个从xls读取的数据集,看起来像这样:

x<-c("c1", 1, 2, 3, "c2", 2, 6, 8, "c3", 4, 3, 2)
x<-as.data.frame(x)

How can I reorganize it to this format in R: 如何在R中将其重组为这种格式:

c1 c2 c3
1 2 4
2 6 3
3 8 2
m <- matrix(c('c1','1','2','3','c2','2','6','8','c3','4','3','2'), nrow = 4, ncol = 3)  
df <- setNames(data.frame(m[-1,]),m[1,])

> df
  c1 c2 c3
1  1  2  4
2  2  6  3
3  3  8  2

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

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