Sum variable by group and append result

2019-02-25 22:55发布

问题:

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))