r语言 - 计算函数内加权平均值时的误差"'x' and 'w' must have the same length"



我有一个这样的数据集:

df = read.table(text='    location total year TR  TY  TU TJ
A     822400 2010 0.09 0.09 0.07    0.07
A     822400 2010 0.13 0.08 0.08    0.06
B     822400 2010 0.18 0.07 0.10    0.05
B     565000 2009 0.05 0.05 0.04    0.04
B     565000 2009 0.07 0.04 0.04    0.03
A     565000 2008 0.10 0.03 0.05    0.02',header=T)

我想使用一个函数,按年份和属性(TR、TY、TU或TJ(计算这两个位置的总加权平均值。为此,我写道:

total.weighted.mean <- function(df, properties, years){

dff<-filter(df, year==years)

res<-dff%>%
group_by(location) %>% 
mutate(wt = weighted.mean(total, properties))

print(res)

}
total.weighted.mean( df, properties = "TR", years = 2009:2010)

但我在函数中遇到了这个错误:

Error in weighted.mean.default(total, properties) : 
'x' and 'w' must have the same length 

当我从函数中计算出来时,我得到了:

location  total  year    TR    TY    TU    TJ     wt
<chr>     <int> <int> <dbl> <dbl> <dbl> <dbl>  <dbl>
1 A        822400  2010  0.13  0.08  0.08  0.06 732310
2 B        565000  2009  0.07  0.04  0.04  0.03 732310

由于不同位置的总值不同,因此每个位置的重量相同正确吗?

主要问题是将weights变量作为字符串传递。要告诉dplyr你指的是数据集中的变量,你可以使用.data代词。此外,当过滤多年时,您应该使用%in%而不是==:

library(dplyr)
df = read.table(text='    location total year TR  TY  TU TJ
A     822400 2010 0.09 0.09 0.07    0.07
A     822400 2010 0.13 0.08 0.08    0.06
B     822400 2010 0.18 0.07 0.10    0.05
B     565000 2009 0.05 0.05 0.04    0.04
B     565000 2009 0.07 0.04 0.04    0.03
A     565000 2008 0.10 0.03 0.05    0.02',header=T)
total.weighted.mean <- function(df, properties, years) {

dff<-filter(df, year %in% years)

res<-dff%>%
group_by(location) %>% 
mutate(wt = weighted.mean(total, .data[[properties]]))

res

}
total.weighted.mean( df, properties = "TR", years = 2009:2010)
#> # A tibble: 5 x 8
#> # Groups:   location [2]
#>   location  total  year    TR    TY    TU    TJ     wt
#>   <chr>     <int> <int> <dbl> <dbl> <dbl> <dbl>  <dbl>
#> 1 A        822400  2010  0.09  0.09  0.07  0.07 822400
#> 2 A        822400  2010  0.13  0.08  0.08  0.06 822400
#> 3 B        822400  2010  0.18  0.07  0.1   0.05 719440
#> 4 B        565000  2009  0.05  0.05  0.04  0.04 719440
#> 5 B        565000  2009  0.07  0.04  0.04  0.03 719440

显然,获取此错误的一种方法是当权重变量也在"by"语句中时。我尝试了两个不同的权重变量,都是双倍的,但只有一个给出了错误。

相关内容

最新更新