Skip to content

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

2 messages · Paul Miller, David Winsemius

#
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)
? )
#
On 1/10/19 7:00 AM, Paul Miller via R-help wrote:
You apparently expect us to have pkg:dplyr loaded. The appropriate code 
for that would be `library(dplyr)` at the initiation of the example. 
Unlike StackOverflow there is no universal expectation in Rhelp of that 
preparation.

The `Reduce` function could probably be used if you were willing to 
rework two columns into a list that had a sequence of 2 two-element 
lists but it's not the first choice for handling this data situation. 
Instead consider using `mapply` or perhaps `by`. You are also going to 
need to wrap this approach in a programming structure that allows a 
patient-focused result to be computed, either `lapply( split( ... `? or 
its equivalent in the hadleyverse:? `%>% group_by(...`

In the hadleyverse context, you should probably learn to use the purrr 
function `map2` for the two-column conditional processing.