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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


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





What's in column "target_dose"? Do you mean tablet? I assume the unit of tablet is days?
– Maurits Evers
6 mins ago




"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.

Popular posts from this blog

Visual Studio Code: How to configure includePath for better IntelliSense results

Spring cloud config client Could not locate PropertySource

Makefile test if variable is not empty