我严格地需要使用summarise_at来计算加权平均值,并根据另一列的值来加权。
df %>% summarise_at(.vars = vars(FACTOR,tv:`smart tv/console`),
.funs = weighted.mean, w=INVESTMENT, na.rm=TRUE)它总是显示错误:'INVESTMENT' is not found.
然后我试着:
df %>%summarise_at(.vars = vars(FACTOR,tv:`smart tv/console`),
.funs = weighted.mean, w=vars(INVESTMENT), na.rm=TRUE)但在本例中:Evaluation error: 'x' and 'w' must have the same length.
为什么会这样呢?我做错什么了吗?你有解决这个问题的提示吗?谢谢
发布于 2019-03-18 06:27:00
您可以直接在weighted.mean()函数中,在调用funs()时指定权重,如下所示:
data.frame(x=rnorm(100), y=rnorm(100), weight=runif(100)) %>%
summarise_at(vars(x,y), funs(weighted.mean(., w=weight)))https://datascience.stackexchange.com/questions/45148
复制相似问题