R:在给定增长率矩阵的情况下,撤销变量的水平
下午好 假设我有以下数据框:R:在给定增长率矩阵的情况下,撤销变量的水平,r,R,下午好 假设我有以下数据框: > df <- data.frame(l=c(1,1.2, 1.56, 1.72, 2.06, 2.68, NA,NA,NA), g1=c(1.1,1.2,1.3,1.1,1.2,1.3,1.1,1.2,1.3), g2=c(1.1,1.2,1.3,1.1,1.2,1.3,1.1,1.2,1.3)) df这应该行得通 df$newl <- c(df$l[1] , df$l[1] * cumprod(df$g1[-1])) 请注意,df$l可
> df <- data.frame(l=c(1,1.2, 1.56, 1.72, 2.06, 2.68, NA,NA,NA), g1=c(1.1,1.2,1.3,1.1,1.2,1.3,1.1,1.2,1.3), g2=c(1.1,1.2,1.3,1.1,1.2,1.3,1.1,1.2,1.3))
df这应该行得通
df$newl <- c(df$l[1] , df$l[1] * cumprod(df$g1[-1]))
请注意,df$l
可以使用
df$l[-nrow(df)] * df$g1[-1]
但这并不意味着失踪
还要注意计算df$l
# the second value is
1.00 * 1.2
# the third value is
1.20 * 1.3 == 1.00 * 1.2 * 1.3
# the fourth value is
1.56 * 1.1 == 1.00 * 1.2 * 1.3 * 1.1
等等
因此,我们可以使用df$g1
的累积乘积来计算df$l
。
然后把它们放在一起。类似于
df$newl@user20650的东西怎么样,你应该发布这个,这是一个不错的答案
# the second value is
1.00 * 1.2
# the third value is
1.20 * 1.3 == 1.00 * 1.2 * 1.3
# the fourth value is
1.56 * 1.1 == 1.00 * 1.2 * 1.3 * 1.1