How to sum a variable by group in R

+4 votes

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

 

Apr 13, 2018 in Data Analytics by Dynamic_Coder22
• 180 points
78,211 views
how could I use mutate in dplyr to add variable x into the dataframe

Hi, @Jackiechan,

mutate(): compute and add new variables into a data table. It preserves existing variables. Look at the below picture for better understanding.

Eg: my_data %>% mutate(data_frama, X = [existing_var])

I hope this will be helpful.

3 answers to this question.

0 votes

Easily by using Aggregate Func():


aggregate(x$points, by=list(Players=x$Players), FUN=sum)

or By Using tapply:

tapply(x$points, x$Players, FUN=sum)
answered Apr 13, 2018 by CodingByHeart77
• 3,750 points

edited Apr 13, 2018 by CodingByHeart77
+1 vote

You can also try this way,

x_new = x %>% group_by(Players) %>% summarise(x = sum(points))

Become a proficient Data Analyst with our comprehensive Data Analyst Course.

answered Aug 1, 2019 by Cherukuri
• 33,030 points
The first operator, i.e., the one after x_new, should be <- not =.
0 votes

An advantage of the aggregate function is that it is already included in your Base R installation. Therefore we do not need to install any add-on packages.

The aggregate function can be used to calculate the summation of each group as follows.

aggregate(x = iris$Sepal.Length,               
          by = list(iris$Species),              
          FUN = sum) 
answered Dec 10, 2020 by MD
• 95,460 points

Related Questions In Data Analytics

0 votes
2 answers

How to use group by for multiple columns in dplyr, using string vector input in R?

data = data.frame(   zzz11def = sample(LETTERS[1:3], 100, replace=TRUE),   zbc123qws1 ...READ MORE

answered Aug 6, 2019 in Data Analytics by anonymous
14,122 views
0 votes
1 answer

How to find out the sum/mean for multiple variables per group in R?

You can use the reshape2 package for ...READ MORE

answered Apr 12, 2018 in Data Analytics by DataKing99
• 8,250 points
3,864 views
0 votes
1 answer

How to create dummy variables based on a categorical variable of lists in R?

You can use mtabulate in the following way: library(qdapTools) cbind(data[1], ...READ MORE

answered Apr 13, 2018 in Data Analytics by CodingByHeart77
• 3,750 points
2,660 views
0 votes
1 answer

How can I convert a factor variable to numeric in R ?

A factor variable can be converted to ...READ MORE

answered Sep 19, 2018 in Data Analytics by shams
• 3,670 points
3,845 views
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,964 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,402 views
0 votes
1 answer

How to spilt a column of a data frame into multiple columns

it is easily achievable by using "stringr" ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,819 views
0 votes
1 answer

How to replace NA values in a dataframe with Zero's ?

It is simple and easy: df1<-as.data.frame(matrix(sample(c(NA, 1:10), 100, ...READ MORE

answered Apr 10, 2018 in Data Analytics by CodingByHeart77
• 3,750 points
2,082 views
+1 vote
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,842 views
+1 vote
3 answers

How to change the value of a variable using R programming in a data frame?

Try this: df$symbol <- as.character(df$symbol) df$symbol[df$sym ...READ MORE

answered Jan 11, 2019 in Data Analytics by Tyrion anex
• 8,700 points
35,873 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP