我具有此功能,可以根据提供的MES和REG_ID列查找值(因数):
factor_estacionalidad <- function(
MES,
REG_ID = 99,
# this is the parameter table, hardly ever changed
estacionalidad = data.frame(
mes = as.integer(rep(1:12, 5)),
REG_ID = as.integer(rep(c(0:3,99), each =12)),
factor = c(
1.46, 1.06, 0.98, 1.10, 1.01, 1.00, 1.45, 1.00, 0.98, 1.00, 1.00, 1.00,
1.42, 1.00, 0.95, 0.94, 0.96, 1.00, 1.40, 1.00, 0.98, 1.00, 1.00, 1.00,
1.46, 1.00, 0.97, 0.97, 0.99, 1.00, 1.42, 1.00, 0.98, 1.00, 1.00, 1.00,
1.46, 1.04, 0.95, 1.01, 1.00, 1.00, 1.42, 1.00, 0.98, 1.00, 1.00, 1.00,
1.46, 1.05, 0.98, 1.10, 1.00, 1.00, 1.44, 1.00, 0.98, 1.00, 1.00, 1.00
)
)
) {
library(dplyr)
mes <- as.integer(MES %% 100)
df <- data.frame(
mes = mes,
REG_ID = REG_ID
) %>%
left_join(
estacionalidad,
by = c("mes", "REG_ID")
)
return(df$factor)
}
假设您拥有下一个df:
df <- data.frame(
MES = 2019 * 100 + rep(as.integer(rep(1:12, 100))),
REG_ID = as.integer(rep(c(0:3), each = 300))
)
您需要将EST列计算为
df <- df %>%
mutate(
EST = factor_estacionalidad(
MES, REG_ID
)
)
我怀疑有一种更好的方法可以对固定表进行双列查找,即进入MUTATE ENVIROMENT。