How to tell group_by to group all data by columns except one?

I tried using group_by(data,-x) but this groups by the negative of -x which means same grouping by x.

Apr 12, 2018 3,144 views

## 1 answer to this question.

You can either use group_by or group_by_at

Using group_by

```# Generate data
set.seed(492)
data = data.frame(value=rnorm(1000), z1=sample(LETTERS,1000,replace=TRUE),                 z2=sample(letters,1000,replace=TRUE), z3=sample(1:10, replace=TRUE),           colour=sample(c("red","green","black"),1000,replace=TRUE))

data %>% group_by_(.dots=names(data)[-grep("value", names(data))]) %>%
summarise(mean_value=mean(value)

#Output
```
``` z1     z2    z3  colour   mean_value
<fctr> <fctr> <int> <fctr>       <dbl>
1       A      a     2  green  0.89281475
2       A      b     2    red -0.03558775
3       A      b     5  black -1.79184218
4       A      c    10  black  0.17518610
5       A      e     5  black  0.25830392
...```

You can also use group_by_at in the following way:

```data %>%
group_by_at(names(data)[-grep("value", names(data))]) %>%
summarise(mean_value=mean(value))

```
```# Groups:   z1, z2, z3 [?]
z1     z2    z3  colour   mean_value
<fctr> <fctr> <int> <fctr>       <dbl>
1      A      a     2  green  0.89281475
2      A      b     2    red -0.03558775
3      A      b     5  black -1.79184218
4      A      c    10  black  0.17518610
5      A      e     5  black  0.25830392
6      A      e     5    red -0.81879788
7      A      e     7  green  0.30836054
8      A      f     2  green  0.05537047
9      A      g     1  black  1.00156405
10      A      g    10  black  1.26884303
# ... with 949 more rows```
• 6,370 points

## How to replace all occurrences of a character in a character column in a data frame in R

If you used sub() to replace the ...READ MORE

## How to fit different elements in a single row or column with varying width of plot or object in shiny application?

How to fit different elements in a ...READ MORE

## How to rename a single column in a data.frame?

data.rename(columns={'gdp':'log(gdp)'}, inplace=True) The rename show that it accepts a dict ...READ MORE

## With the help of tidyverse: how to rename a column to a variable name

With the help of Dplyr: rename function ...READ MORE

## How to sort substring from a dataframe

First, extract the substring from the data ...READ MORE

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

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

## How to create a list of Data frames?

Basically all we have to do is ...READ MORE

## How to spilt a column of a data frame into multiple columns

it is easily achievable by using "stringr" ...READ MORE

+1 vote

## How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

+1 vote

## How to convert a list of dataframes in to a single dataframe using R?

You can use the plyr function: data <- ...READ MORE