Using R statistics add a group sum to each row [duplicate]

Use ave:

 df$sumx1 <- ave(df$x1, df$key, FUN=sum)

Leave a Comment