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)

Any help would be greatly appreciated. Apr 12, 2018 1,654 views

## 1 answer to this question.

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
• 8,210 points

## 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

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

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

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

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

## How to sum a variable by group in R?

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

+1 vote

## 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

## In data frame how to spilt strings into values?

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

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

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