In the below dataset, if Loc==1
, I want to calculate a value, but if Loc!=1
, I want to calculate a value based on the value I calculated at Loc==1
, then based on the value calcuated at Loc==2
, etc. I know I can accomplish this in a loop, but I'm trying to avoid loops if I can help it. It should look something like this, note that the previous
object doesn't actually exist, it's just a placeholder to better explain what I'm looking for.
data2 <- data %>%
mutate(Residual = ifelse(Loc==1,
pmin(XD_VD+SE, val), # first/previous calc
pmin(previous + SE, val))) # second calc based on first calc
I tried this:
data2 <- data %>%
mutate(Residual = pmin(XD_VD + SE, val),
Residual = ifelse(Loc==1,
Residual,
pmin(lag(Residual,1)+SE, value))
However, this doesn't work as it calculates the equation based on Residual
and not on the previous calculation. If anyone knows a way to do this, that would be most appreciated! Here is a reprex of my data:
data <- structure(list(Loc = 1:5, val = c(29, 49, 44, 50, 37), XD = c(66.12,
66.12, 66.12, 66.12, 66.12), VD = c(30.1780172853143, 30.1780172853143,
30.1780172853143, 30.1780172853143, 30.1780172853143), XD_VD = c(35.9419827146857,
35.9419827146857, 35.9419827146857, 35.9419827146857, 35.9419827146857
), SE = c(0.77, 0.77, 0.77, 0.77, 0.77)), row.names = c(NA, -5L
), class = c("tbl_df", "tbl", "data.frame"))