多列的加权平均值
Posted
技术标签:
【中文标题】多列的加权平均值【英文标题】:Weighted Mean for multiple columns 【发布时间】:2021-09-03 09:51:41 【问题描述】:我试图在多列中找到每年(每一行)的加权平均值。 然而,它只给了我一个所有 45 列和所有年份的数字......
我试过了,
Weightedmean = Max_Year %>%
summarise(weighted.mean(Max_Year[,13:58], c(0.004961,0.005119,0.01368,0.01609,0.01623,0.005966,0.05848,
0.01964, 0.01979,0.002283,0.003003,0.005639,0.02502,0.006511,
0.005004,0.003045, 0.005926,0.004474,0.00746,0.004598,0.009667,
0.002236,0.01318,0.002836,0.002751,0.002607,0.00745,0.00349,0.02161,
0.002247,0.01697,0.005194,0.00678,0.01313,0.04428,0.004778,0.002424,
0.0044,0.00365,0.08852,0.01103,0.005788,0.008653,0.02242,0.002705,0.007384))
but all i get is this
weighted.mean(...)
1 8736。
What am I doing wrong? Thank you!
【问题讨论】:
【参考方案1】:我们可以使用across
Max_Year %>%
mutate(wt = c(0.004961,0.005119,0.01368,0.01609,0.01623,0.005966,0.05848,
0.01964, 0.01979,0.002283,0.003003,0.005639,0.02502,0.006511,
0.005004,0.003045, 0.005926,0.004474,0.00746,0.004598,0.009667,
0.002236,0.01318,0.002836,0.002751,0.002607,0.00745,0.00349,0.02161,
0.002247,0.01697,0.005194,0.00678,0.01313,0.04428,0.004778,0.002424,
0.0044,0.00365,0.08852,0.01103,0.005788,0.008653,0.02242,0.002705,0.007384)) %>%
group_by(year) %>%
summarise(across(13:58, ~ weighted.mean(., wt))
【讨论】:
以上是关于多列的加权平均值的主要内容,如果未能解决你的问题,请参考以下文章