How to sum per row the last digit when shift 2 ends , not always will be same amount of row until shift two sometimes can be 5 other can be 10 rows until we can reach shift 2

B <- tibble(Day =rep(1:2 ,times=10,each=1),
            TC = c("compact","compact","compact", "compact", 
                   "compact ","compact", "compact", "compact", "compact ","compact","compact","compact","compact", "compact", 
                   "compact ","compact", "compact", "compact", "compact ","compact"),
            Trash_left = c(100,300,8800,600,20000,400000,500,600000,50000.7,4000,10000,
                           300,30000,600,20000,400000,500,600000,100.7,4000),
            shift= c("1","1","1","1","1","2","2","2","2","2","1","1","1","1","1","2","2","2","2","2"))

T1<- filter(B,Day==1 )
 # A tibble: 10 × 4
     Day TC         Trash_left shift
   <int> <chr>           <dbl> <chr>
 1     1 "compact"        100  1    
 2     1 "compact"       8800  1    
 3     1 "compact "     20000  1    
 4     1 "compact"        500  2    
 5     1 "compact "     50001. 2    
 6     1 "compact"      10000  1    
 7     1 "compact"      30000  1    
 8     1 "compact "     20000  1    
 9     1 "compact"        500  2    
10     1 "compact "       101. 2

The result will be
50001.+101. =50102

So you want to find the rows where the current value of shift is 2, but where the next value is not 2. We can use lead() to look at the next value.

T1 |>
  mutate(next_shift = lead(shift))

If you do that however, the last value will be NA (since there is no row below), and that will be a problem afterwards. So we set a default non-NA value:

T1 |>
  mutate(next_shift = lead(shift, default = "none"),
         shift_2_end = (shift == 2) & (next_shift != 2))

And done, you can just sum what you want:

library(tidyverse)
B <- tibble(Day =rep(1:2 ,times=10,each=1),
            TC = c("compact","compact","compact", "compact", 
                   "compact ","compact", "compact", "compact", "compact ","compact","compact","compact","compact", "compact", 
                   "compact ","compact", "compact", "compact", "compact ","compact"),
            Trash_left = c(100,300,8800,600,20000,400000,500,600000,50000.7,4000,10000,
                           300,30000,600,20000,400000,500,600000,100.7,4000),
            shift= c("1","1","1","1","1","2","2","2","2","2","1","1","1","1","1","2","2","2","2","2"))

T1<- filter(B,Day==1 )

T1 |>
  mutate(next_shift = lead(shift, default = "none"),
         shift_2_end = (shift == 2) & (next_shift != 2)) |>
  filter(shift_2_end) |>
  pull(Trash_left) |>
  sum()
#> [1] 50101.4

B |>
  group_by(Day) |>
  mutate(next_shift = lead(shift, default = "none"),
         shift_2_end = (shift == 2) & (next_shift != 2)) |>
  filter(shift_2_end) |>
  summarize(sum_Trash = sum(Trash_left))
#> # A tibble: 2 × 2
#>     Day sum_Trash
#>   <int>     <dbl>
#> 1     1    50101.
#> 2     2     8000

Created on 2023-04-05 with reprex v2.0.2

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.