列表列表的一个向量的平均值

Mean of one vector of list of lists

我有以下数据集

myList <- split(x = ChickWeight, f = ChickWeight$Diet)

我想按列表计算权重的平均值,即四个不同的平均值。一种可能的解决方案是

a<-lapply(myList, `[[`, 1)
lapply(a, mean)

但如果我能在“a”中有平均功能,是否可能呢?即

a<-lapply(myList, `[[`, 1, mean)

使用匿名函数-

lapply(myList, function(x) mean(x$weight))
#Also by position of the column
#lapply(myList, function(x) mean(x[[1]]))

#$`1`
#[1] 102.6455

#$`2`
#[1] 122.6167

#$`3`
#[1] 142.95

#$`4`
#[1] 135.2627

如果数据集尚未拆分,您可以使用 aggregate

aggregate(weight~Diet, ChickWeight, mean)

您可以定义一个函数 colMean 以在 lapply 中使用。

colMean <- function(x, col) colMeans(x[, col, drop=FALSE])

lapply(myList, colMean, col='weight')  ## also: `col=1`
# $`1`
# weight 
# 102.6455 
# 
# $`2`
# weight 
# 122.6167 
# 
# $`3`
# weight 
# 142.95 
# 
# $`4`
# weight 
# 135.2627 

也适用于多列。

lapply(myList, colMean, col=c('weight', 'Time')
# $`1`
# weight      Time 
# 102.64545  10.48182 
# 
# $`2`
# weight      Time 
# 122.61667  10.91667 
# 
# $`3`
# weight      Time 
# 142.95000  10.91667 
# 
# $`4`
# weight      Time 
# 135.26271  10.75424 

使用tidyverse

library(purrr)
library(dplyr)
map(myList, ~ .x %>%
     pull(weight) %>%
     mean)

-输出

$`1`
[1] 102.6455

$`2`
[1] 122.6167

$`3`
[1] 142.95

$`4`
[1] 135.2627

或使用原始数据base R

tapply(ChickWeight$weight, ChickWeight$Diet, FUN = mean)
       1        2        3        4 
102.6455 122.6167 142.9500 135.2627