So i have a complexe problem where I have to simulate the change of statu over time (25 years) my dataframe presented like this:
age | sex | edu | statu | exp_y | sal | cost | sal_a | cost_a |
---|---|---|---|---|---|---|---|---|
26 | f | no | em | 2 | 76 | 100 | 0 | 0 |
20 | m | low | unem | 0 | 0 | 0 | 287 | 150 |
24 | m | low | unem | 2 | 0 | 0 | 345 | 200 |
24 | m | no | em | 10 | 380 | 150 | 0 | 0 |
36 | f | hiegh | em | 18 | 684 | 300 | 0 | 0 |
42 | f | low | em | 17 | 646 | 245 | 0 | 0 |
change conditions are :
if (statu=="unem" & sal_a>cost_a) then update the values of these variables {statu = "em",age=age+1, exp_y=exp_y+1, sal=sal_a*(1+0.19*exp_y), cost=cost_a, sal_a=0 and cost_a=0}
if (statu=="em" & sal<cost) so {statu = "unem" ; age=age+1, sal_a=sal, cost_a=cost, exp_y=exp_y, sal=0 and cost=0}
Any ideas pleas,
kind regards
You can try this:
# Number of years
y <- 25
# Create a list
evol <- list()
# Make "y" copies of your df
for (i in 1:y){evol[[i]] <- df}
# At each step (strting in the second element) evaluate the changes
for (i in 2:y){
# Logical vector (including the positions of rows to change)
v.logic <- evol[[i-1]]$statu=="unem" & evol[[i-1]]$sal_a> evol[[i]]$cost_a
v.logic2 <- evol[[i-1]]$statu=="em" & evol[[i-1]]$sal<evol[[i-1]]$cost
# Values to change
# Fist condition
evol[[i]][v.logic,"age"] <- evol[[i-1]][v.logic,"age"] + 1
evol[[i]][v.logic,"exp_y"] <- evol[[i-1]][v.logic,"exp_y"] + 1
evol[[i]][v.logic,"sal"] <- evol[[i-1]][v.logic,"sal_a"]*(1+0.19*evol[[i-1]][v.logic,"exp_y"])
evol[[i]][v.logic, "cost"] <- evol[[i-1]][v.logic,"cost_a"]
evol[[i]][v.logic, "sal_a"] <- 0
evol[[i]][v.logic, "cost_a"] <- 0
# Second condition
evol[[i]][v.logic2,"statu"] <- "unem"
evol[[i]][v.logic2, "age"] <- evol[[i-1]][v.logic2, "age"] + 1
evol[[i]][v.logic2, "sal_a"] <- evol[[i-1]][v.logic2, "sal"]
evol[[i]][v.logic2, "cost_a"] <- evol[[i-1]][v.logic2, "cost"]
evol[[i]][v.logic2, "sal"] <- 0
evol[[i]][v.logic2, "cost"] <- 0
}
It will result on a list where the i-th element represents the table for the i-th year.