【发布时间】:2017-08-19 22:53:28
【问题描述】:
【问题讨论】:
-
请尝试将示例数据发布为文本,而不是图像;它使提供答案变得更容易。
-
当然@neilfws...
-
请修改您的标题,它目前的信息量非常少。
标签: r data-manipulation
【问题讨论】:
标签: r data-manipulation
我们可以使用recast进行数据转换
library(reshape2)
recast(df1, id.var = "Company", Company ~ value, length)
# Company CRV diesel Grande Petrol
#1 Honda 1 0 0 1
#2 Tata 0 1 1 0
df1 <- data.frame(Company = c("Honda", "Tata"), model = c("CRV", "Grande"),
version = c("Petrol", "diesel"), stringsAsFactors= FALSE)
【讨论】:
看来,您实际上是按列制表,因此您应该也可以使用table,而无需额外的包。
do.call(cbind, lapply(df1[-1], function(x) table(df1[[1]], x)))
## CRV Grande diesel Petrol
## Honda 1 0 0 1
## Tata 0 1 1 0
这样,相关列也被分组在一起,而不是按字母顺序排序。
【讨论】: