This is a sample dataset. I would like to write a for-loop for this sequence such that the loop starts at create and ends at a submit or cancel. If there is a create, after which there isn't a submit or cancel, then that create request was abandoned. If there are consecutive creates, then the last one was a successful one.
df <- structure(list(
sess_id = c(189, 189, 189, 189, 167, 167, 167, 167, 124, 124, 124, 124),
user_id = c("504", "504", "504", "504", "504", "504", "504", "504", "505", "505", "505", "505"),
activity = c("create", "submit", "create", "cancel", "create", "submit", "submit", "create", "create", "submit", "submit", "cancel")),
.Names = c("sess_id", "user_id", "activity"),
row.names = c(NA, -12L),
class = "data.frame")
I would like the dataset to look like this:
df_want <- structure(list(
sess_id = c(189, 189, 189, 189, 167, 167, 167, 167, 124, 124, 124, 124),
user_id = c("504", "504", "504", "504", "504", "504", "504", "504", "505", "505", "505", "505"),
activity = c("create", "submit", "create", "cancel", "create", "submit", "submit", "create", "create", "submit", "submit", "cancel"),
status = c("success_create", "success_submit", "cancel_create", "success_cancel", "success_create", "unsuccess_submit", "success_submit", "abandon_create", "cancel_create", "unsuccess_submit", "unsuccess_submit", "success_cancel")),
.Names = c("sess_id", "user_id", "activity", "status"),
row.names = c(NA, -12L),
class = "data.frame")
Since this is an iterative process, I think for-loops would be helpful. But, if there are other ways of doing it, for example, by using the apply() function, I would love to read those solutions too.