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) ) ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.