T3 <- data.frame(
TC = c("COMPACT", "COMPACT", "COMPACT", "COMPACT", "COMPACT", "COMPACT", "COMPACT", "COMPACT"),
truck_id = c(18, 18, 18, 18, 18, 18, 18, 18),
VARIANCE = c("LOW", "LOW", "LOW", "LOW", "LOW", "LOW", "LOW", "LOW"),
TRASH_AMOUNT = c("HIGH", "HIGH", "HIGH", "HIGH", "HIGH", "HIGH", "HIGH", "HIGH"),
shift2 = c(1, 1, 1, 1, 2, 2, 2, 2),
thrash_left_in_all_blocks = c(26273.3, 21282.3, 17028.3, 12633.9, 18419.3, 17391.4, 14900.5, 13089.8),
thrash_left_in_block = c(0, 0, 0, 0, 0, 0, 0, 0),
amount_of_thrash_collected = c(1540.6, 1043, 1123.9, 1004.4, 665, 338.2, 1069.7, 627)
)
T3
> ```
> TC truck_id VARIANCE TRASH_AMOUNT shift2 thrash_left_in_all_blocks thrash_left_in_block
> 1 COMPACT 18 LOW HIGH 1 26273.3 0
> 2 COMPACT 18 LOW HIGH 1 21282.3 0
> 3 COMPACT 18 LOW HIGH 1 17028.3 0
> 4 COMPACT 18 LOW HIGH 1 12633.9 0
> 5 COMPACT 18 LOW HIGH 2 18419.3 0
> 6 COMPACT 18 LOW HIGH 2 17391.4 0
> 7 COMPACT 18 LOW HIGH 2 14900.5 0
> 8 COMPACT 18 LOW HIGH 2 13089.8 0
> amount_of_thrash_collected thrash_left_in_all_blocks_2 last_thrash_value
> 1 1540.60 26273.300 0.067
> 2 1043.00 25230.300 0.067
> 3 1123.90 24106.400 0.067
> 4 24106.33 0.067 0.067
> 5 665.00 18419.300 16384.400
> 6 338.20 18081.100 16384.400
> 7 1069.70 17011.400 16384.400
> 8 627.00 16384.400 16384.400
> >
> ```
I want to subtract the thrash left in all blocks minus the amount of thrash collected column but starting from the first row of thrash left in all blocks minus the second row of amount_of_thrash_collected until shift 1 ends. After that, the next shift 2 will do the same.
I did this
T3 <- T3 %>%
mutate(thrash_left_in_all_blocks_2 = NA, # Añade una nueva columna con NA para los cálculos
last_thrash_value = NA) # Añade una nueva columna para almacenar el último valor de cada grupo
for (shift_value in unique(T3$shift2)) {
shift_rows <- which(T3$shift2 == shift_value)
resta1 <- T3$thrash_left_in_all_blocks[shift_rows[1]]
T3$thrash_left_in_all_blocks_2[shift_rows[1]] <- resta1
for (i in 2:length(shift_rows)) {
row_index <- shift_rows[i]
resta1 <- resta1 - T3$amount_of_thrash_collected[row_index]
T3$thrash_left_in_all_blocks_2[row_index] <- resta1
}
T3$last_thrash_value[T3$shift2 == shift_value] <- resta1
}
print(T3)
The problems are when in the real data frame it reaches the subtraction with an1.7E16 as answer it failed on the subtraction, and it does not place that last value as last_thrash_value and the next shift calculation is wrong. How could I fixed this ?