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,690 points
91 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 darklord
• 6,170 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 28 in Data Analytics by anonymous
• 31,840 points
84 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 49 views
0 votes
1 answer
0 votes
1 answer

How to sort substring from a dataframe

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

answered Nov 4 in Data Analytics by Cherukuri
• 31,840 points
33 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
161 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
105 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
109 views
0 votes
2 answers

How to sort a data frame by columns in R?

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

answered Aug 21 in Data Analytics by anonymous
• 31,840 points
151 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 13, 2018 in Data Analytics by darklord
• 6,170 points
194 views