How to group all columns exculding a single column

0 votes

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 in Data Analytics by CodingByHeart77
• 3,750 points
3,528 views

1 answer to this question.

0 votes

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
answered Apr 12, 2018 by Sahiti
• 6,370 points

Related Questions In Data Analytics

0 votes
1 answer

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

answered Jun 29, 2019 in Data Analytics by anonymous
• 33,030 points
17,936 views
0 votes
0 answers
0 votes
1 answer

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

answered Jun 24, 2022 in Data Analytics by Sohail
• 3,040 points
408 views
0 votes
1 answer

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

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

answered Apr 3, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Apr 3, 2018 by DeepCoder786 946 views
0 votes
1 answer

How to sort substring from a dataframe

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

answered Nov 4, 2019 in Data Analytics by Cherukuri
• 33,030 points
1,117 views
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,920 views
0 votes
1 answer

How to create a list of Data frames?

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

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,341 views
0 votes
1 answer

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

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

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,775 views
+1 vote
2 answers

How to sort a data frame by columns in R?

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

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,812 views
+1 vote
1 answer

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

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

answered Apr 14, 2018 in Data Analytics by Sahiti
• 6,370 points
7,038 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