Sum variable by group and append result

后端 未结 1 1125
天命终不由人
天命终不由人 2021-01-22 03:50

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

Student   Friend       nCalls
TJ        Spinelli                


        
1条回答
  •  旧时难觅i
    2021-01-22 04:01

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

    0 讨论(0)
提交回复
热议问题