lsangha
lsangha

Reputation: 39

Sum based on condition of Rolling sum

I want to get the sum of the numbers but first checking the rolling sum for two consecutive values.

In another context, I have precipitation data and if more than 3 inches of rainfall occurred over two days I want 3 inches to be added in the final sum.

Here if df contains precipitation data I want the sum of df but if the rolling sum of two consecutive days increases 3, it should reduce the sum of those two numbers to 3 while calculating the final sum.

e.g. sum of the first two numbers is 2.78 (1.79996688 + 0.99847062), it should use existing numbers. The next sum is 2.45 (0.99847062 +1.465839), it should use the existing numbers. The next 2.86342148+1.44870719 is 4.3 which is greater than 3 the value taken in the final sum for these two days should be 3.

I want one single value in the end. The answer in the above case should be1.79996688 + 0.99847062+0.99847062 +1.465839 + 3+ and so on.

set.seed(123)
df <- c(runif(100, min = 0, max = 3))
df

Upvotes: 0

Views: 257

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 389325

You can use rollsum from zoo to perform rolling calculation. If the sum of 2 numbers is greater than 3, we use pmin to restrict the values at 3. Finally, we take sum of all the values to get 1 number at the end as result.

set.seed(123)
data <- runif(100, min = 0, max = 3)
result <- sum(pmin(zoo::rollsum(data, 2), 3))
result
#[1] 246.5425

Upvotes: 1

Tiberius
Tiberius

Reputation: 11

If I'm understanding your question correctly, you want a rolling sum of the previous value + the current value. Then, if that sum is greater than 3, you want to "clip" the values so that the maximum value is 3. I made this into a dataframe with several steps to be clear about what I was accomplishing at each step:

set.seed(123)
df <- data.frame(x = c(runif(100, min = 0, max = 3)))
df <- df %>%
  mutate(tot = lag(x, 1, default = 0) + x,
         final = case_when(tot >= 3 ~  3,
                           tot <  3 ~ tot)) 

df %>% summarise(totalprecip = sum(final, na.rm = TRUE))

If this isn't what you were trying to accomplish, just clarify.

Upvotes: 1

Related Questions