Tpellirn
Tpellirn

Reputation: 796

how to compute the mean of dataframe in a list?

  list_data1 <- data.frame(date=c("Jan", "Feb", "Mar"), X0=c(3, 9, 5, 1, -2, 8),
                     X1=c(3, 9, 5, 1, -2, 8), mod=c("gg", "gg", "gg"))
  list_data2 <- data.frame(date=c("Jan", "Feb", "Mar"), X0=c(3, 9, 5, 1, -2, 8),
                     X1=c(3, 9, 5, 1, -2, 8), mod=c("m", "m", "m"))
  list_data3 <- data.frame(date=c("Jan", "Feb", "Mar"), X0=c(3, 9, 5, 1, -2, 8),
                     X1=c(3, 9, 5, 1, -2, 8), mod=c("d", "d", "d"))
  list_data4 <- data.frame(date=c("Jan", "Feb", "Mar"), X0=c(1, 2, 5, 1, -2, 8),
                     X1=c(3, 9, 5, 1, -2, 8), mod=c("b", "b", "b"))
  list_data5 <- data.frame(date=c("Jan", "Feb", "Mar"), X0=c(3, 1, 5, 1, -2, 8),
                     X1=c(1, 2, 3, 1, -2, 8), mod=c("f", "f", "f"))
  merged.list <- list(list_data1, list_data2,list_data3,list_data4,list_data5)

I want to compute the mean of all numeric columns X0 with X0, X1 with X1, etc for the mod f and b [lets call it mod1], then compute the mean of all numeric columns for the rest of mod [lets call it mod2]

Output example:

  [[1]] # mean of values of b and f
  date  X0 X1  mod
  Jan    2   2    mod1
  Feb    1.5  5.5    mod1
  [[2]] # mean of values of the rest (m,gg,d)
  date X0 X1  mod
  Jan  ?  ?    mod2
  Feb   ?  ?    mod2  

Upvotes: 2

Views: 61

Answers (2)

akrun
akrun

Reputation: 887118

We can use map_if

library(dplyr)
library(purrr)
map_if(merged.list, .p = ~ first(.x$mod) %in% c('b', 'f'), ~ 
     .x %>% 
        group_by(date) %>%
        summarise(across(where(is.numeric), mean)), .else = NA_character_) %>%
     keep(negate(is.null))
#[[1]]
# A tibble: 3 x 3
#  date     X0    X1
#* <chr> <dbl> <dbl>
#1 Feb     0     3.5
#2 Jan     1     2  
#3 Mar     6.5   6.5

#[[2]]
# A tibble: 3 x 3
#  date     X0    X1
#* <chr> <dbl> <dbl>
#1 Feb    -0.5   0  
#2 Jan     2     1  
#3 Mar     6.5   5.5

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388982

Combine the data into one dataframe (combine_data), divide the data into two list, one with 'b' and 'f' values and other with rest of the values and calculate mean for each month.

combine_data <- do.call(rbind, merged.list)

lapply(split(combine_data, combine_data$mod %in% c('b', 'f')), function(x) {
  aggregate(cbind(X0, X1)~date, x, mean)
})

Using tidyverse functions :

library(dplyr)
library(purrr)

bind_rows(merged.list) %>%
  group_split(mod %in% c('b', 'f'), .keep = FALSE) %>%
  map(~.x %>% group_by(date) %>% summarise(across(X0:X1, mean, na.rm = TRUE)))

Upvotes: 2

Related Questions