gpt4 book ai didi

r - 计算以零为底的累积总和 (cumsum)

转载 作者:行者123 更新时间:2023-12-04 01:14:59 27 4
gpt4 key购买 nike

我想修改 cumsum 函数。我想将负值更改为 0。并且当有一个不同于 0 的新 D 值时,则使用 D-S

下面的例子:

TD <- data.frame(product = rep("A", 7),
data = seq(as.Date("2020-01-01"), as.Date("2020-01-07"), by = "day"),
D = c(74, 0, 0, 0, 0, 20, 10), S = c(20, 30, 20, 5, 2, 4, 5))

TD <- TD %>% group_by(product) %>% mutate(result = cumsum(D) - cumsum(S))

> TD
# A tibble: 6 x 5
# Groups: product [1]
product data D S result I expected
<chr> <date> <dbl> <dbl> <dbl>
1 A 2020-01-01 74 20 54 54
2 A 2020-01-02 0 30 24 24
3 A 2020-01-03 0 20 4 4
4 A 2020-01-04 0 5 -1 0
5 A 2020-01-05 0 2 -3 0
6 A 2020-01-06 20 4 13 16
7 A 2020-01-07 10 5 18 21

最佳答案

我认为这个功能可以满足您的需求

pos_cumsum <- function(x) {
cs <- cumsum(x)
cm <- cummin(cs)
return (cs - pmin(cm, 0))
}

TD<- TD%>% group_by(product) %>% mutate(result = pos_cumsum(D-S))
TD
#> # A tibble: 7 x 5
#> # Groups: product [1]
#> product data D S result
#> <chr> <date> <dbl> <dbl> <dbl>
#> 1 A 2020-01-01 74 20 54
#> 2 A 2020-01-02 0 30 24
#> 3 A 2020-01-03 0 20 4
#> 4 A 2020-01-04 0 5 0
#> 5 A 2020-01-05 0 2 0
#> 6 A 2020-01-06 20 4 16
#> 7 A 2020-01-07 10 5 21

虽然我想知道 D 是发生在 S 之前还是之后......

关于r - 计算以零为底的累积总和 (cumsum),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63636052/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com