How to sum a variable by group in R

542    Asked by bhagwatidubey in Python , Asked on Apr 22, 2021
I have a data frame of consisting two columns"Players" & "points"x<-data.frame(Players=c("Player1","Player2","Player3","Player2","Player3","Player1"),points=c(10,20,30,13,17,18))

Players points

1 Player1     10
2 Player2     20
3 Player3     30
4 Player2     13
5 Player3     17
6 Player1     18

I want to sort the data by players and sum the points:

Like this:

Players  x
1 Player1 28
2 Player2 33
3 Player3 47


Answered by Benjamin Moore
  To sum by group in r, you can use the group_by and the summarise_at functions to get the summation by group: iris %>% # Specify data frame group_by(Species) %>% # Specify group indicator summarise_at(vars(Sepal. Length), # Specify column list(name = sum)) # Specify function # A tibble: 3 x 2 # Species name #   # 1 setosa 250.


Your Answer

Interviews

Parent Categories