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
• 420 points
3,332 views

1 answer to this question.

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,240 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
13,625 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,790 points
518 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 Sahiti
• 6,370 points
1,175 views
+4 votes
3 answers

How to sum a variable by group in R?

You can also try this way, x_new = ...READ MORE

answered Aug 1, 2019 in Data Analytics by Cherukuri
• 33,030 points
77,167 views
+1 vote
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,720 points
1,229 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 5, 2018 in Data Analytics by Kalgi
• 52,360 points
743 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,720 points
1,589 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 26, 2018 in Data Analytics by DataKing99
• 8,240 points
546 views
0 votes
2 answers

How to set working directory to source file location in R?

Hi,  I'm not sure about the command but you ...READ MORE

answered Aug 20, 2019 in Data Analytics by Cherukuri
• 33,030 points
13,363 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