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
• 410 points
384 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,130 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 5 in Data Analytics by anonymous
3,195 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
30 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,170 points
131 views
0 votes
2 answers

How to sum a variable by group in R?

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

answered Jul 31 in Data Analytics by Cherukuri
• 28,280 points
8,959 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
105 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
• 41,760 points
55 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
135 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,130 points
56 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,130 points
280 views