这些功能都不是特别难做的,但是我想知道如何组合它们。
df <- tibble::tibble(index = seq(1:8),
amps = c(7, 6, 7, 0, 7, 6, 0, 6))
只要放大器的价值为正,我想将它们加起来。如果amps = 0,则表示序列中断,我想返回0,然后重新开始。我也想返回相应的索引值。结果将如下所示:
index amps
<dbl> <dbl>
1 1 20
2 4 0
3 5 13
4 7 0
5 8 6
我可以在VBA中做到这一点,但我想增强我在函数式编程中的R技能。我宁愿使用函数而不是循环,因为它们更干净。任何帮助表示赞赏。
Using
dplyr
:One
dplyr
option could be: