我有以下資料集
myList <- split(x = ChickWeight, f = ChickWeight$Diet)
我想按串列計算權重的平均值,即四個不同的平均值。一種可能的解決方案是
a<-lapply(myList, `[[`, 1)
lapply(a, mean)
但是,如果我可以在“a”中具有平均功能,這可能嗎?IE
a<-lapply(myList, `[[`, 1, mean)
uj5u.com熱心網友回復:
使用匿名函式 -
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)
uj5u.com熱心網友回復:
您可以定義一個函式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
uj5u.com熱心網友回復:
使用 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/322608.html
