我有如下示例数据:
library(diagis) # weighted_se
table_selection <- structure(list(year = c(2006, 2006, 2006, 2006, 2006), Totaal_pop_weights = c(12.125,
12.125, 12.125, 12.125, 12.125), Y02_pop_weights = c(97, 97,
97, 97, 97), Y01_pop_weights = c(12.125, 12.125, 12.125, 12.125,
12.125), h10_pop_weights = c(12.125, 12.125, 12.125, 12.125,
12.125), A_ha_pop_weights = c(12.125, 12.125, 12.125, 12.125,
12.125), B_ha_pop_weights = c(12.125, 12.125, 12.125, 12.125,
12.125), C_ha_pop_weights = c(97, 97, 97, 97, 97), D_ha_pop_weights = c(12.125,
12.125, 12.125, 12.125, 12.125), variable = structure(c(2L, 1L,
1L, 4L, 1L), levels = c("A_ha", "B_ha", "C_ha",
"C_ha", "Y01", "Y02", "Totaal", "X10"), class = "factor"),
value = c(2, 3, 1, 1, 12.9)), row.names = c(NA, -5L), class = c("data.table",
"data.frame"))
year Totaal_pop_weights Y02_pop_weights Y01_pop_weights h10_pop_weights A_ha_pop_weights B_ha_pop_weights
1: 2006 12.125 97 12.125 12.125 12.125 12.125
2: 2006 12.125 97 12.125 12.125 12.125 12.125
3: 2006 12.125 97 12.125 12.125 12.125 12.125
4: 2006 12.125 97 12.125 12.125 12.125 12.125
5: 2006 12.125 97 12.125 12.125 12.125 12.125
C_ha_pop_weights D_ha_pop_weights variable value
1: 97 12.125 B_ha 2.0
2: 97 12.125 A_ha 3.0
3: 97 12.125 A_ha 1.0
4: 97 12.125 C_ha 1.0
5: 97 12.125 A_ha 12.9
我想对这些意见作如下权衡:
weights_of_interest <- select(table_selection, contains(c("weights")))
table_selection <- table_selection %>%
group_by(year, variable) %>%
summarize(weighted_mean = weighted_mean(value, w = Y01_pop_weights , na.rm=TRUE),
weighted_se = weighted_se(value, w = Y01_pop_weights , na.rm=TRUE))
但是它一直使用相同的权重,我怎么改变权重,使得变量为A_ha
的值,使用A_ha_pop_weights
作为权重。
2条答案
按热度按时间0ve6wy6x1#
如果
table_selection
是一个data.table(如示例数据所示),则可以创建一个新的单列wt
,根据variable
中的值保存弹出权重值下面是使用
dplyr
(rowwise()
和cacross()
)的相同方法无论使用哪种方法,您都可以在上面对
summarize()
的调用中使用w=wt
。wfsdck302#
如果你想要一个tidyverse的解决方案,我认为最好的方法是使用tidyr把数据转换成长格式。我的计算机不知道函数'weighted_mean'或'weighted_se',所以我不能100%肯定这会工作。
但是使用统计数据包中的加权平均值...
退货: