'Calculating mean of each data frame inside a list of lists and assigning it to a new vector in R
This is my data:
df1 <- data.frame(x = 1:5, y = letters[1:5])
df2 <- data.frame(x = 1:15, y = letters[1:15])
df3 <- data.frame(x = 1:25, y = letters[1:25])
df4 <- data.frame(x = 1:6, y = letters[1:6])
df5 <- data.frame(x = 1:8, y = letters[1:8])
l1 <- list(df1, df2)
l2 <- list(df3, df4, df5)
mylist <- list(l1, l2)
I want to calculate the mean of the x column in all data frames inside mylist, and put them in a new empty list (or vector), like so:
mean_vec <- c(
mean(df1$x),
mean(df2$x),
mean(df3$x),
mean(df4$x),
mean(df5$x)
)
Solution 1:[1]
Another possible solution, based on purrr::map_depth:
library(tidyverse)
map_depth(mylist, 2, ~ mean(.x$x)) %>% unlist
#> [1] 3.0 8.0 13.0 3.5 4.5
Or using rrapply::rrapply, solution that is now shorter thanks to @Maƫl's comment, to whom I thank:
library(rrapply)
library(magrittr)
rrapply(mylist, condition = is.numeric, f = mean, how = "unlist") %>% unname
#> [1] 3.0 8.0 13.0 3.5 4.5
Solution 2:[2]
You can unlist your nested list and compute the mean for each:
mean_vec <- sapply(unlist(mylist, recursive = F), function(dat) mean(dat$x))
mean_vec
# [1] 3.0 8.0 13.0 3.5 4.5
Another option with rapply:
mean <- rapply(mylist, mean)
unname(mean[names(mean) == "x"])
# [1] 3.0 8.0 13.0 3.5 4.5
Solution 3:[3]
A purrr solution
library(purrr)
library(dplyr)
mylist %>%
map_depth(., 2, ~ .x %>% summarise(mean = mean(x, na.rm = T))) %>%
bind_rows() %>%
pull()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | Julian |
