简体   繁体   English

如何从长到宽重塑和归纳分类数据?

[英]How to reshape and summarise categorical data from long to wide?

My data base is like this one: 我的数据库是这样的:

db <- data.frame(var1 = c("A", "B", "C", "D", "E"), var2 = c("X", "X", "Y", "Y", "Y"),
           var3 = c("G", "H", "G", "G", "K"))
db

  var1 var2 var3
    A    X    G
    B    X    H
    C    Y    G
    D    Y    G
    E    Y    K

I'd like to reshape based in var2 and count the occurrences of var3 to get this result: 我想基于var2重塑形状并计算var3的出现次数以获得以下结果:

  var2 var3.G var3.H var3.K
    X    1      1      0
    Y    2      0      1

I have tried the cast and the reshape functions with no success. 我尝试了投射和重塑功能,但均未成功。

The xtabs function is reasonably simple to use. xtabs函数使用起来相当简单。 The only cognitive jump is to realize that there is no LHS unless you want to do summation of a third variable: 唯一的认知上的飞跃是认识到没有LHS,除非您想对第三个变量求和:

> xtabs( ~var2+var3, data=db)
    var3
var2 G H K
   X 1 1 0
   Y 2 0 1

You don't want to do as.data.frame on this since it will convert to long form but you can use as.data.frame.matrix on it, since an R-'table' inherits from the 'matrix' class. 您不希望对此执行as.data.frame ,因为它将转换为长格式,但是您可以在其上使用as.data.frame.matrix ,因为R-'table'继承自'matrix'类。

tbl <- data.frame( var2 = db[,2], var3 = paste("var3", db[,3], sep = "."))
table(tbl)
    var3
var2 var3.G var3.H var3.K
   X      1      1      0
   Y      2      0      1

One more option. 还有一个选择。 Using the super useful data.table package: 使用超级有用的data.table包:

library(data.table)

db <- data.table(var1 = c("A", "B", "C", "D", "E"), var2 = c("X", "X", "Y", "Y", "Y"),
           var3 = c("G", "H", "G", "G", "K"))

dcast.data.table(db, var2 ~ var3, fun = length, value.var= 'var3')
   var2 G H K
1:    X 1 1 0
2:    Y 2 0 1

Here is another way go about it: 这是另一种解决方法:

You can use a combination of t() and table(). 您可以结合使用t()和table()。

db <- data.frame(var1 = c("A", "B", "C", "D", "E"), 
                 var2 = c("X", "X", "Y", "Y", "Y"),
                 var3 = c("G", "H", "G", "G", "K"))
db

t(table(db$var3,db$var2))

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

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