按组对变量求和并追加结果

Sum variable by group and append result

数据集 HAVE 是 phone 从 Recess:

的字符调用数据的 tibble edgelist
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

数据集 NEED 包括来自 HAVE 的所有原始列,但包括一个新变量 nCallsPerStudent,这正是它听起来的样子:

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

如何从 HAVENEED

我们可以按 'student' 和 mutate 分组来创建新列

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

或使用base R

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