r - 带有分类变量的 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 中获取四种组合中的每一种的表。知道出了什么问题吗?非常感谢。
解决方案
我们可以做到这一点fable
ftable(mtcars[c('carb', 'vs', 'am')])
推荐阅读
- json - Json 信息转换成 div 组件 react
- java - 如何在 Spring MVC 中将整数方法转换为布尔值
- python - 我在 mac 上安装 pandas 时遇到问题
- c# - ASP.NET Core 5.0 中通过 CSS 实现的背景图像
- html - 如何防止 CSS 过滤器有时导致 HTML 元素重新定位?
- laradock - 在 Laradock 上为 Browsershot 安装 chromium
- python-3.x - 如何根据python中的自定义优先级比较变量?
- html - 在 iframe 中隐藏 PDF 的侧视图
- c - 创建一个文本文件并在其中写入 char 数组的地址
- android - 在没有 Google api 的 Kotlin 中轮询 Android 上的 GPS 位置