in R: Sum by group without summarising [duplicate]

☆樱花仙子☆ 提交于 2019-12-25 04:49:14

问题


I have searched a lot, but not found a solution.

I have the following data frame:

 Age no.observations Factor
1    1               4      A
2    1               3      A
3    1              12      A
4    1               5      B
5    1               9      B
6    1               3      B
7    2              12      A
8    2               3      A
9    2               6      A
10   2               7      B
11   2               9      B
12   2               1      B

I would like to sum create another column with the sum by the categories Age and Factor, thus having 19 for the first three rows, 26 for the next three etc. I want this to be a column added to this data.frame, therefore dplyr and its summarise function do not help.


回答1:


Use mutate with group_by to not summarise:

df %>%
  group_by(Age, Factor) %>%
  mutate(no.observations.in.group = sum(no.observations)) %>%
  ungroup()


来源:https://stackoverflow.com/questions/39594279/in-r-sum-by-group-without-summarising

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!