Search code examples
rlistdplyraverage

r groupby mean from list of objects


From a list of object like this. I am trying to estimate the mean by column x

   [[1]]

             ymin     ymax     x
             42.34721 47.26508 0.125
             43.85269 46.66316 0.375

  [[2]]

             ymin     ymax     x
             42.47198 47.33008 0.125
             43.89134 46.68878 0.375

  [[3]]

             ymin     ymax     x
             42.48128 47.35456 0.125
             43.89433 46.69511 0.375
            

I am expecting the mean to be estimated like this

      ymin                                    ymax                                 x  
      (42.34721 + 42.47198 + 42.48128)/3     (47.26508 + 47.33008 + 47.35456)/3    0.125
      (43.85269 + 43.89134 + 43.89433)/3     (46.66316 + 46.68878 + 46.69511)/3    0.375

I tried several options for example

       df <- do.call(rbind, listdf)
       aggregate (df[1:2], df[3], mean)

       listdf %>%
       group_by(3) %>%
       summarise_all(mean) %>%
       as.data.frame()

       aggregate(. ~ x, data = df, FUN=mean)

I am not getting the right answer from none of these attempts above. Any suggestions or advise on where I am wrong is much appreciated. Thanks.


Solution

  • We could do it this way (with dplyr):

    library(dplyr)
    
    list_of_dataframes <- list(
      data.frame(
        ymin = c(42.34721, 43.85269),
        ymax = c(47.26508, 46.66316),
        x = c(0.125, 0.375)
      ),
      data.frame(
        ymin = c(42.47198, 43.89134),
        ymax = c(47.33008, 46.68878),
        x = c(0.125, 0.375)
      ),
      data.frame(
        ymin = c(42.48128, 43.89433),
        ymax = c(47.35456, 46.69511),
        x = c(0.125, 0.375)
      )
    )
    
    list_of_dataframes %>%
      bind_rows() %>% 
      summarise(ymin_mean = mean(ymin), ymax_mean = mean(ymax), .by = x)
    
      x ymin_mean ymax_mean
    1 0.125  42.43349  47.31657
    2 0.375  43.87945  46.68235