【发布时间】:2016-09-01 15:58:13
【问题描述】:
假设我有一个这样的数据框
> dat
a b c
1 1 0.3321008 0.3321008
2 2 -0.2946729 NA
3 3 -0.1447266 NA
4 4 -0.9415429 NA
5 5 -1.0165080 NA
这里是dput
structure(list(a = 1:5, b = c(0.332100835317822, -0.294672931641969,
-0.144726592564241, -0.941542877670977, -1.0165079846083), c = c(0.332100835317822,
NA, NA, NA, NA)), .Names = c("a", "b", "c"), row.names = c(NA,
-5L), class = "data.frame")
我想对c 列执行操作,使得c = lag(c)*b(c 中的第一个元素除外
我可以使用下面的简单 for 循环来做到这一点
for(i in (1:4)){
dat$c[i+1] <- dat$c[i]*dat$b[i+1]
}
输出:
> dat
a b c
1 1 0.3321008 0.33210084
2 2 -0.2946729 -0.09786113
3 3 -0.1447266 0.01416311
4 4 -0.9415429 -0.01333517
5 5 -1.0165080 0.01355531
如何使用 dplyr mutate 做到这一点?还是使用应用函数?
【问题讨论】:
-
无论是否在dplyr,你似乎都需要
dat$c[1]*cumprod(replace(dat$b, 1, 1))