I have some data frames in a list with all having the same structure – in this example the variables a, b and c. Now I want to summarize the means of the values across the list.
# list of 10 random data frames
n <- 1e1
initSeed <- 1234
set.seed(initSeed)
(seedVec <- sample.int(n = 1e3, size = n, replace = FALSE))
lst <- lapply(1:n, function(i){
set.seed(seedVec[i])
a <- rnorm(24,1,.1)
b <- rnorm(24,2,.2)
c <- rnorm(24,3,.3)
df <- data.frame(a,b,c)
})
I attempted to feed dplyr with lst %>% summarize_all(mean)
but he won't like lists. The formula below gives me the means of each data frame in the list, but not yet the means of these variables a, b and c across all data frames.
lapply(1:10, function(n){
lst[n] %>%
data.frame() %>%
summarize_all(mean)
})
So i wanted to make a new data frame with the summarized outputs in order to summarize them again, but this fails and both my extended formula and a related answer are throwing the Error in lst[[idx]] : subscript out of bounds
Here is my attempt:
df1 <- as.data.frame(setNames(replicate(3,numeric(0), simplify = FALSE),
letters[1:3]))
lapply(1:10, function(n){
lst[n] %>%
data.frame() %>%
summarize_all(mean) %>%
rbind(df1, lst[n])
})
df1 %>% summarize_all(mean)
How could I get what I want?