在R中聚合具有多个函数参数的函数 [英] Aggregate function in R with multiple function arguments
问题描述
我有样本data.set,其中包含不同季节的气候数据:
I have sample data.set containing climate data for different seasons:
df <- data.frame(season=rep(1:5,2),year=rep(1:2,each=5),
temp=c(2,4,3,5,2,4,1,5,4,3),ppt=c(4,3,1,5,6,2,1,2,2,2),
samples=c(22,25,24,31,31,29,28,31,30,32))
我可以简单地确定每年每个季节的气候变量平均值:
I can determine the mean of my climate variables for each season for each year simply:
aggregate(df[,c('temp','ppt')], by = list(df$season,df$year), function(x) mean(x,na.rm=T))
但是,我想使用变量样本
作为我的权重,确定每个季节|年份组合的加权平均值。
However, I want to determine the weighted mean of each season|year combo using variable samples
as my weights.
基本上我想用 aggregate()
中的平均值
函数> weighted.mean 。这将需要在我的函数中添加第二个参数,该参数需要使用我的 x
进行更改。
Essentially I want to replace my mean
function in aggregate()
with weighted.mean
. That would require adding a second argument to my function that needs to change with my x
.
function(x,w) weighted.mean(x,w,na.rm=T))
不过,我不确定如何让的权重参数('w')加权.mean()
因汇总数据的每个子集而异。
Though, I'm not sure how to let the weight argument ('w') of weighted.mean()
vary with each subset of the aggregated data.
我可以在 aggregate
函数?
任何建议都很棒!
推荐答案
尝试使用 dplyr
中的 summarise_each
。它允许使用 group_by
进行先前的分组,并应用于多个列:
Try summarise_each
from dplyr
. It allows for the prior grouping with group_by
and application to multiple columns:
library(dplyr)
df %>% group_by(season, year) %>%
summarise_each(funs(weighted.mean(., samples,na.rm=T)), temp,ppt)
# Source: local data frame [10 x 5]
# Groups: season, year [10]
#
# season year temp ppt samples
# (int) (int) (dbl) (dbl) (dbl)
# 1 1 1 2 4 22
# 2 2 1 4 3 25
# 3 3 1 3 1 24
# 4 4 1 5 5 31
# 5 5 1 2 6 31
# 6 1 2 4 2 29
# 7 2 2 1 1 28
# 8 3 2 5 2 31
# 9 4 2 4 2 30
# 10 5 2 3 2 32
这篇关于在R中聚合具有多个函数参数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!