我从xls读取的数据集如下所示:
x<-c("c1", 1, 2, 3, "c2", 2, 6, 8, "c3", 4, 3, 2)
x<-as.data.frame(x)
如何在R:
中将其重新组织为此格式c1 c2 c3
1 2 4
2 6 3
3 8 2
答案 0 :(得分:4)
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