I have a data-frame likeso:

```
x <-
id1 id2 val1 val2 val3 val4
1 a x 1 9
2 a x 2 4
3 a y 3 5
4 a y 4 9
5 b x 1 7
6 b y 4 4
7 b x 3 9
8 b y 2 8
```

I wish to aggregate the above by id1 & id2. I want to be able to get the means for val1, val2, val3, val4 at the same time.

How do i do this?

This is what i currently have but it works just for 1 column:

```
agg <- aggregate(x$val1, list(id11 = x$id1, id2= x$id2), mean)
names(agg)[3] <- c("val1") # Rename the column
```

Also, how do i rename the columns which are outputted as means in the same statement given above

We can use the formula method of `aggregate`

. The variables on the 'rhs' of `~`

are the grouping variables while the `.`

represents all other variables in the 'df1' (from the example, we assume that we need the `mean`

for all the columns except the grouping), specify the dataset and the function (`mean`

).

```
aggregate(.~id1+id2, df1, mean)
```

Or we can use `summarise_each`

from `dplyr`

after grouping (`group_by`

)

```
library(dplyr)
df1 %>%
group_by(id1, id2) %>%
summarise_each(funs(mean))
```

Or using `summarise`

with `across`

(`dplyr`

devel version - `‘0.8.99.9000’`

)

```
df1 %>%
group_by(id1, id2) %>%
summarise(across(starts_with('val'), mean))
```

Or another option is `data.table`

. We convert the 'data.frame' to 'data.table' (`setDT(df1)`

, grouped by 'id1' and 'id2', we loop through the subset of data.table (`.SD`

) and get the `mean`

.

```
library(data.table)
setDT(df1)[, lapply(.SD, mean), by = .(id1, id2)]
```

```
df1 <- structure(list(id1 = c("a", "a", "a", "a", "b", "b",
"b", "b"
), id2 = c("x", "x", "y", "y", "x", "y", "x", "y"),
val1 = c(1L,
2L, 3L, 4L, 1L, 4L, 3L, 2L), val2 = c(9L, 4L, 5L, 9L, 7L, 4L,
9L, 8L)), .Names = c("id1", "id2", "val1", "val2"),
class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8"))
```

- How to get AIC from Conway–Maxwell-Poisson regression via COM-poisson package in R?
- R : how to simply repeat a command?
- session not created: This version of ChromeDriver only supports Chrome version 74 error with ChromeDriver Chrome using Selenium
- How to show code but hide output in RMarkdown?
- remove kernel on jupyter notebook
- Function to calculate R2 (R-squared) in R
- Center Plot title in ggplot2
- R ggplot2: stat_count() must not be used with a y aesthetic error in Bar graph
- R multiple conditions in if statement
- What does "The following object is masked from 'package:xxx'" mean?
- Saving a high resolution image in R
- Change bar plot colour in geom_bar with ggplot2 in r
- Converting data frame column from character to numeric
- Extract Month and Year From Date in R
- How to combine two lists in R
- Extract year from date
- Ifelse statement in R with multiple conditions
- R dplyr: Drop multiple columns
- Remove legend ggplot 2.2
- Remove all of x axis labels in ggplot
- how to remove multiple columns in r dataframe?
- Aggregate multiple columns at once
- Changing fonts in ggplot2
- How to specify "does not contain" in dplyr filter
- how to use the Box-Cox power transformation in R
- Convert dataframe column to 1 or 0 for "true"/"false" values and assign to dataframe
- Having trouble setting working directory
- Coerce multiple columns to factors at once
- How to declare a vector of zeros in R
- Create empty data frame with column names by assigning a string vector?
- Explain ggplot2 warning: "Removed k rows containing missing values"
- R for loop skip to next iteration ifelse
- Error: package or namespace load failed for ggplot2 and for data.table
- How do I change the default library path for R packages
- Select first and last row from grouped data
- R * not meaningful for factors ERROR
- Error: could not find function "%>%"
- Raise to power in R
- In R, dealing with Error: ggplot2 doesn't know how to deal with data of class numeric
- Error - replacement has [x] rows, data has [y]
- Merge r brings error "'by' must specify uniquely valid columns"
- Non-numeric Argument to Binary Operator Error in R
- Convert row names into first column
- Append data frames together in a for loop
- Plotting with ggplot2: "Error: Discrete value supplied to continuous scale" on categorical y-axis
- R Markdown - changing font size and font type in html output
- Replace all occurrences of a string in a data frame
- How to convert dataframe into time series?
- Sum across multiple columns with dplyr
- Removing NA observations with dplyr::filter()
- [Move to Aggregate multiple columns at once]

- Pandas group-by and sum
- SELECT list is not in GROUP BY clause and contains nonaggregated column
- Aggregate multiple columns at once
- Pandas sum by groupby, but exclude certain columns
- Extract the maximum value within each group in a dataframe
- How to group dataframe rows into list in pandas groupby
- Mean per group in a data.frame
- Summarizing multiple columns with dplyr?
- data.frame Group By column
- Compute mean and standard deviation by group for multiple variables in a data.frame
- SQL Server "cannot perform an aggregate function on an expression containing an aggregate or a subquery", but Sybase can
- ListAGG in SQLSERVER
- Select the top N values by group
- Multiple aggregations of the same column using pandas GroupBy.agg()
- Count number of rows within each group
- Aggregate / summarize multiple variables per group (e.g. sum, mean)
- Count number of rows per group and add result to original data frame
- LINQ Aggregate algorithm explained
- Aggregate a dataframe on a given column and display another column
- C# Linq Group By on multiple columns
- What are Aggregates and PODs and how/why are they special?
- Saving a select count(*) value to an integer (SQL Server)
- How to sum a variable by group
- SQL Server : SUM() of multiple rows including where clauses
- Sql Server : How to use an aggregate function like MAX in a WHERE clause
- SQL JOIN, GROUP BY on three tables to get totals
- [Move to Aggregate multiple columns at once]