Using R Language
I’m trying to calculate new column called balance using 2 columns in the data set and one external value, my point is to use the y (external value) with this calculation to generate new Colum called balance and then use this balance instead of y , i need to update the value for each row.
y <- 4000 # external value
# Create data set
df <- data.frame(id = c(1,1,1,2,2,2),
DR = c(10,20,30,40,50,60),
CR = c(-10,-20,-70,-60,-80,-30))
# used loop
for(i in df$id) {
i <- y
i <- i + df$CR - df$CR
df$balance<- i
}
#result
id DR CR balance
1 1 10 -10 4000
2 1 20 -20 4000
3 1 30 -70 4000
4 2 40 -60 4000
5 2 50 -80 4000
6 2 60 -30 4000
*balance Colum doesn't update*