【发布时间】:2022-01-08 19:36:06
【问题描述】:
我希望每个人都做得很好。我在尝试在 R 中聚合时有点放屁。假设我有这个 df:
| student | subject |
|---|---|
| Amber | math |
| Colin | math |
| Bob | science |
| Amber | math |
| Amber | science |
我想计算学生的科目是数学的次数并将其添加到数据框中,因此结果如下所示:
| student | subject | total 'math' |
|---|---|---|
| Amber | math | 2 |
| Colin | math | 1 |
| Bob | science | 0 |
| Amber | math | 2 |
| Amber | science | 2 |
这可能吗?我尝试了 aggregate(subject["math"] ~ student, data = df, length) 只是为了完成第一部分,但我得到“model.frame.default 中的错误(formula = subject["math"] ~ : variable长度不同(为“学生”找到)”。
提前谢谢你!
【问题讨论】:
-
你想要的是
ave而不是aggregate
标签: r dataframe count aggregate