Search code examples
rdplyrsapply

sapply for each group using dplyr


df <- data.frame(group = rep(1:4, each = 10), 
                   x1 = rnorm(40),  x2 = rnorm(40), x3 = rnorm(40), x4 = rnorm(40), 
                   X5 = rnorm(40), x6 = rnorm(40), x7 = rnorm(40))

sapply(df[, 4:ncol(df)], function(x) sd(x)/mean(x))

I want to apply this function for each group. How do I correct the below command?

df %>% dplyr::group_by(group) %>% do.call(sapply(.[, 4:ncol(.)] function(x) sd(x)/mean(x)))

Solution

  • If I understood your question/objective, the following will give the results you're seeking. It uses the plyr package over the dplyr package. You're likely running into issues using the %>% function with do.call as well, since %>% is just a shortcut for passing the preceding object as the first argument to the subsequent function, and do.call expects a named function as its first argument

    library(plyr)
    
    df <- data.frame(group = rep(1:4, each = 10), 
                     x1 = rnorm(40),  x2 = rnorm(40), x3 = rnorm(40), x4 = rnorm(40), 
                     X5 = rnorm(40), x6 = rnorm(40), x7 = rnorm(40))
    
    ddply(df,.(group),function(x) 
      { 
        sapply(x[,4:ncol(x)],function(y) sd(y)/mean(y))
      })
    

    Gives the following results

     group        x3        x4        X5         x6        x7
    1     1  1.650401 -1.591829  1.509770   6.464991  3.520367
    2     2 11.491301 -2.326737 -1.725810 -11.712510  2.293093
    3     3 -3.623159 -1.416755  2.958689   1.629667 -4.318230
    4     4  9.169641 -4.219095  2.083300   1.985500 -1.678107