1

I have many variables to group and produce mean value. I have given the r code below. Any other way to give the code short.

a<-b %>%
group_by(c,d,e,f,g,h,i,j,k,l) %>%
summarize(numbobs =n(),m= mean(m),n= mean(n),o= mean(o),p= mean(p),q= mean(q),r= mean(r)s= mean(s)) 
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
111111
  • 67
  • 1
  • 1
  • 5
  • @ProcrastinatusMaximus Don't you think it is different from the dupe link as this also include `numobs` and passing variables etc.. – akrun Jun 21 '16 at 09:53
  • 1
    @akrun you are correct, [this one](http://stackoverflow.com/questions/12064202/using-aggregate-to-apply-several-functions-on-several-variables-in-one-call) is better I think – Jaap Jun 21 '16 at 09:57

1 Answers1

0

We can do a couple of things to make it short.

1) By using group_by_, we can group by many variables by passing an object that have the column names ('nm1')

2) Use summarise_each for applying the same function (mean) to multiple columns.

nm1 <- letters[3:12]
b %>%
   group_by_(.dots = nm1) %>%
   mutate(n = n()) %>%
   group_by(n, add=TRUE) %>%
   summarise_each(funs(mean), m:s) 

data

set.seed(24)
m1 <- matrix(rnorm(10000*7), nrow=10000, dimnames = list(NULL, letters[13:19]))
set.seed(42)
m2 <- matrix(sample(letters[1:5], 10000*10, replace=TRUE), nrow=10000, 
         dimnames = list(NULL, letters[3:12]))
b <- cbind(as.data.frame(m2, stringsAsFactors=FALSE), m1)
b[3:5, 1:10] <- b[1, 1:10]
akrun
  • 874,273
  • 37
  • 540
  • 662