根据评论,我会调整我的问题,尽量让它更容易理解。问题仍然是相同的,并显示我目前计算它的方式。以下是包含起始列的较短数据:
dput(my.df)
structure(list(date = structure(c(16831, 16860, 16891, 16921,
16952, 16982, 17013, 17044, 17074, 17105, 17135, 17166, 17197,
17225, 17256, 17286, 17317, 17347, 17378, 17409, 17439, 17470,
17500, 17531, 17562, 17590, 17621, 17651, 17682, 17712, 17743,
17774, 17804), class = "Date"), price = c(98.67, 98.83, 100.07,
100.11, 100.47, 100.63, 100.06, 100.14, 100.54, 100.8, 100.7,
101.26, 100.39, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA), fcst.yoy.growth = c(NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1.61, NA, NA,
1.55, NA, NA, 1.58, NA, NA, 1.47, NA, NA, 1.37, NA, NA, 1.41,
NA, NA, 1.51)), row.names = 194:226, class = "data.frame", .Names = c("date",
"price", "fcst.yoy.growth"))
我有price
指数和未来增长同比预测。我想解决未来日期的价格指数。我一直在解决它:
在原始价格指数滞后的情况下创建价格指数并填写原始的NAs:
my.df$growth1 <- (1+as.numeric(my.df$fcst.yoy.growth)/100) * as.numeric(lag(my.df$price, 12))
my.df$growth1[is.na(my.df$growth1)] <- my.df$price[is.na(my.df$growth1)]
再次做同样的事情以获得完整的结果:
my.df$growth2 <- (1+as.numeric(my.df$fcst.yoy.growth)/100) * as.numeric(lag(my.df$growth1, 12))
my.df$growth2[is.na(my.df$growth2)] <- my.df$price[is.na(my.df$growth2)]
增长2是我想要的结果。