How to change the value in a row if condition met in the previous row under dplyr

Multi tool use


How to change the value in a row if condition met in the previous row under dplyr
Is there any alternative way to replace the following codes under dplyr to avoid explicit loop and data name to achieve the following?
This is to create an adjusted date, if the condition of the current supp_date less than the previous supp_date + tablet is met.
test$adj_fill_dt <- as.Date(NA, "%Y-%m-%d")
test$adj_fill_dt[1] <- test$supp_date[1]
for(i in 2:6) {
if (test[i, "supp_date"] < test[i-1, "adj_fill_dt"] + test[i-1, "tablet"]) {
test[i, "adj_fill_dt"] <- test[i-1, "adj_fill_dt"] + test[i-1, "tablet"]
} else {
test[i, "adj_fill_dt"] <- test[i, "supp_date"]
}
}
From:
supp_date tablet
2017-07-19 30
2017-08-07 30
2017-09-08 30
2017-10-11 30
2017-11-08 30
2017-12-07 30
To:
supp_date tablet adj_fill_dt
2017-07-19 30 2017-07-19
2017-08-07 30 2017-08-18
2017-09-08 30 2017-09-17
2017-10-11 30 2017-10-17
2017-11-08 30 2017-11-16
2017-12-07 30 2017-12-16
"target_dose"
tablet
tablet
@MauritsEvers Sorry about that. Did some editing on the code, forgot to change it. And yes, you can assume that.
– Fred
4 mins ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What's in column
"target_dose"
? Do you meantablet
? I assume the unit oftablet
is days?– Maurits Evers
6 mins ago