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

0 votes

Is there any way to find the sum, mean max for multiple variables simultaneously?

Consider the sample data below:

library(lubridate)
days = 365*2
date = seq(as.Date("2001-02-02"), length = days, by = "day")
year = year(date)
month = month(date)
a1 = cumsum(rnorm(days, 0.05)) 
a2 = cumsum(rnorm(days, 0.05))
data1 = data.frame(date, year, month, a1, a2)

I want to find the aggregate of a1 and a2  variables from the data2 data frame by year and month.

The below code aggregates the a1 variable, but is it possible to simultaneously aggregate the a2 variable.

# aggregate variables by year month
data2=aggregate(a1 ~ year+month, data=data1, sum, na.rm=TRUE)
head(data2)

Any help would be greatly appreciated.

Apr 12, 2018 in Data Analytics by BHARANI
• 400 points
177 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

You can use the reshape2 package for this:

require(reshape2)
df <- melt(df1, id = c("date", "year", "month"))
dcast(df, year + month ~ variable, sum)
#  year month         a1           a2
1  2001     1  -80.83405 -224.9540159
2  2001     2 -223.76331 -288.2418017
3  2001     3 -188.83930 -481.5601913
4  2001     4 -197.47797 -473.7137420
5  2001     5 -259.07928 -372.4563522
answered Apr 12, 2018 by DataKing99
• 8,100 points

Related Questions In Data Analytics

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,640 views
0 votes
1 answer

How to find out the package version which is loaded in R?

You can use sessionInfo() to accomplish that. > ...READ MORE

answered Apr 18, 2018 in Data Analytics by zombie
• 3,690 points
14 views
0 votes
1 answer

How to find out cluster center mean of DBSCAN in R?

Just index back into the original data ...READ MORE

answered Jun 27, 2018 in Data Analytics by darklord
• 6,140 points
62 views
0 votes
1 answer

How to sum a variable by group in R?

Easily by using Aggregate Func(): aggregate(x$points, by=list(Players=x$Players), FUN=sum) or ...READ MORE

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

edited Apr 13, 2018 by CodingByHeart77 3,246 views
0 votes
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,700 points
31 views
0 votes
2 answers

In data frame how to spilt strings into values?

You can do this using dplyr and ...READ MORE

answered Dec 4, 2018 in Data Analytics by Kalgi
• 35,750 points
21 views
0 votes
1 answer
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
60 views
0 votes
1 answer

How to cluster center mean of DBSCAN in R?

Just index back into the original data ...READ MORE

answered Jun 25, 2018 in Data Analytics by DataKing99
• 8,100 points
31 views
0 votes
1 answer

What are the options for deploying models in production with R?

Well, I could say that the answer ...READ MORE

answered Apr 12, 2018 in Data Analytics by DataKing99
• 8,100 points
127 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.