Sum variable by group and append result

强颜欢笑 提交于 2019-12-02 02:15:41

问题


Dataset HAVE is a tibble edgelist of phone call data from the characters of Recess:

Student   Friend       nCalls
TJ        Spinelli          3
TJ        Gretchen          7
TJ        Gus               6
TJ        Vince             8
TJ        King Bob          1
TJ        Mikey             2
Spinelli  TJ                3
Spinelli  Vince             2
Randall   Ms. Finster      17

Dataset NEED includes all original columns from HAVE but includes a new variable, nCallsPerStudent, that is exactly what it sounds like:

Student   Friend       nCalls   nCallsPerStudent
TJ        Spinelli          3                 27
TJ        Gretchen          7                 27
TJ        Gus               6                 27
TJ        Vince             8                 27
TJ        King Bob          1                 27
TJ        Mikey             2                 27
Spinelli  TJ                3                  5
Spinelli  Vince             2                  5
Randall   Ms. Finster      17                 17

How do I get from HAVE to NEED?


回答1:


We can group by 'student' and mutate to create the new column

library(dplyr)
df %>%
  group_by(Student) %>%
  mutate(nCallsPerStudent = sum(nCalls))

Or using base R

df$nCallsPerStudent <- with(df, ave(nCalls, Student, FUN = sum))


来源:https://stackoverflow.com/questions/52260293/sum-variable-by-group-and-append-result

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