我想创建一个新列(val_new),其中每个值乘以另一列(val2)中的值。当我想对多个组执行此操作时,我希望使用dplyr
,但是如何?
dat <- data.frame(group = rep(c("A", "B"), each = 3),
val1 = c(50, NA, NA, 40, NA, NA),
val2 = c(NA, 0.5, 0.3, NA, 0.8, 0.7))
> dat
group val1 val2
1 A 50 NA
2 A NA 0.5
3 A NA 0.3
4 B 40 NA
5 B NA 0.8
6 B NA 0.7
dat %>%
group_by(group) %>%
mutate(val_new = ifelse(!is.na(val1), val1, lag(val_new) * val2))
Error in mutate_impl(.data, dots) :
Evaluation error: object 'val_new' not found.
所需结果:
# A tibble: 6 x 4
# Groups: group [2]
group val1 val2 val_new
<fct> <dbl> <dbl> <dbl>
1 A 50 NA 50
2 A NA 0.5 25
3 A NA 0.3 7.5
4 B 40 NA 40
5 B NA 0.8 32
6 B NA 0.7 22.4
答案 0 :(得分:2)
尝试一下:
dat %>%
group_by(group) %>%
mutate(val_new = cumprod(c(first(val1),val2[-1])))
## A tibble: 6 x 4
## Groups: group [2]
# group val1 val2 val_new
# <fct> <dbl> <dbl> <dbl>
#1 A 50 NA 50
#2 A NA 0.5 25
#3 A NA 0.3 7.5
#4 B 40 NA 40
#5 B NA 0.8 32
#6 B NA 0.7 22.4