Skip to content
Back to formatted view

Raw Message

Message-ID: <1437053605.9401019.1547132445763@mail.yahoo.com>
Date: 2019-01-10T15:00:45Z
From: Paul Miller
Subject: Running list of drugs taken and dropped (via Reduce and accumulate = TRUE or by other means)
In-Reply-To: <mailman.353172.1.1547118002.60835.r-help@r-project.org>

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)
? )