[R] Running list of drugs taken and dropped (via Reduce and accumulate = TRUE or by other means)

Paul Miller pjmiller_57 @ending from y@hoo@com
Thu Jan 10 16:00:45 CET 2019


Hello All,

Would like to keep a running total of what drugs cancer patients have taken and what drugs have been dropped. Searched the Internet and found a way to cumulatively paste a series of drug names. Am having trouble figuring out how to make the paste conditional though. 

Below is some sample data and code. I'd like to get the paste in the "taken" column to add a drug only when change = 1. I'd also like to get the paste in the "dropped" column to add a drug only when change = -1. 

Thanks,

Paul


sample_data <-
  structure(
    list(
      PTNO = c(82320L, 82320L, 82320L),
      change = c(1, 1, -1),
      drug = c("cetuximab", "docetaxel", "cetuximab")),
    class = c("tbl_df", "tbl", "data.frame"),
    row.names = c(NA, -3L)
  ) %>%
  mutate(
    taken = Reduce(function(x1, x2) paste(x1, x2, sep = ", "), drug, accumulate = TRUE),
    dropped = Reduce(function(x1, x2) paste(x1, x2, sep = ", "), drug, accumulate = TRUE)
  )



More information about the R-help mailing list