How to sum a variable by group in R?

0 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
• 140 points
3,282 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,680 points

edited Apr 13, 2018 by CodingByHeart77

Related Questions In Data Analytics

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,100 points
182 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 18, 2018 in Data Analytics by shams
• 3,580 points
20 views
0 votes
1 answer

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$symb ...READ MORE

answered Jan 11 in Data Analytics by Tyrion anex
• 8,280 points
47 views
0 votes
1 answer

How to join two tables (tibbles) by *list* columns in R

You can use the hash from digest ...READ MORE

answered Apr 5, 2018 in Data Analytics by kappa3010
• 2,010 points
36 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,700 points
61 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,700 points
32 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,700 points
23 views
0 votes
1 answer

How to sort a data frame by columns in R?

You can just use the order function ...READ MORE

answered Apr 10, 2018 in Data Analytics by darklord
• 6,140 points
52 views
0 votes
1 answer

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

dplyr added versions for group_by. This allows you ...READ MORE

answered Apr 12, 2018 in Data Analytics by CodingByHeart77
• 3,680 points

edited Apr 12, 2018 by CodingByHeart77 1,654 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,680 points
310 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.