我正在尝试使用tapply()对R中的mtcar数据集进行一些描述性分析。
所以问题是:
> 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通过在mtcar$carb上使用tapply,我希望从vs和am获得四个组合中每一个组合的表知道哪里出问题了吗?非常感谢。
发布于 2020-06-24 13:12:07
tapply已经完成了计算,但它不能以易于查看的形式提供。您可以将table的输出包装在list中。
tapply(mtcars$carb,list(mtcars$vs,mtcars$am),function(x) list(table(x)))
#[[1]]
#x
#2 3 4
#4 3 5
#[[2]]
#x
#1 2 4
#3 2 2
#[[3]]
#x
#2 4 6 8
#1 3 1 1
#[[4]]
#x
#1 2
#4 3 或者使用lapply:
temp <- tapply(mtcars$carb,list(mtcars$vs,mtcars$am),table)
lapply(temp, I)发布于 2020-06-25 05:13:01
我们可以使用fable做到这一点
ftable(mtcars[c('carb', 'vs', 'am')])https://stackoverflow.com/questions/62547907
复制相似问题