0

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.

jpsmith
  • 11,023
  • 5
  • 15
  • 36
user2845095
  • 465
  • 2
  • 9
  • I dont think you need a loop here, you can likely do this with vectorization since it appears to be simply logic-based. Look into `dplyr`'s `lead` and `lag`. If you have tried anything, I would suggest editing your question to include that code as well – jpsmith Nov 18 '22 at 15:04

0 Answers0