首页 > 解决方案 > 带有分类变量的 tapply

问题描述

我正在尝试使用 tapply() 进行一些描述性分析,并使用 R 中的 mtcars 数据集。

所以问题是:

> table(mtcars$carb)

 1  2  3  4  6  8 
 7 10  3 10  1  1 
> tapply(mtcars$carb,list(mtcars$vs,mtcars$am),function(x){length(x)})
   0 1
0 12 6
1  7 7

上面的行有效,但下面的行没有:

> tapply(mtcars$carb,list(mtcars$vs,mtcars$am),function(x){table(x)})
  0         1        
0 Integer,3 Integer,4
1 Integer,3 Integer,2

通过在 mtcars$carb 上使用 tapply,我希望从 vs 和 am 中获取四种组合中的每一种的表。知道出了什么问题吗?非常感谢。

标签: rtapply

解决方案


我们可以做到这一点fable

ftable(mtcars[c('carb', 'vs', 'am')])

推荐阅读