传播数据框以包括新的计数器列R.

时间:2017-06-07 13:10:22

标签: r

我想传播按TYPE(第1列)分组的数据框,以显示与它们相关联的所有不同DESC。我在R工作;我能找到处理这种情况的最接近的函数是tidyr :: spread,但我不希望每个DESC都有不同的列,只是计数器列。

以下代码创建了2个数据框,提供了我想要转换的内容的示例(mydata - > mydata2)

column1 <- c("color","color","color","size","size")
column2 <- c("red","blue","green","small","big")
mydata <- data.frame(column1,column2)
colnames(mydata)[c(1:2)] <- c("type","desc")
View(mydata)

转换为:

column1 <- c("color","size")
column2 <- c("red","big")
column3 <- c("green","small")
column4 <- c("blue",NA)
mydata2 <- data.frame(column1,column2,column3,column4)
colnames(mydata2)[c(1:4)] <- c("type","desc1","desc2","desc3","desc4")
View(mydata2)

0 个答案:

没有答案