Problem: To do some survey analysis on prescription drug use in R, I need to turn multiple rows of the same person (ID) into one, indicating TRUE if any of said rows has TRUE in it.
Here's the data:
df <- data.frame(ID = c("a","a","a","a","a","a"),
cardiovasc = c(T,T,T,T,T,T),
beta_blockers = c(F,F,F,F,F,F),
antibiotics = c(T,F,F,F,F,F),
stringsAsFactors=FALSE)
Here's what I'd like it to look like:
goal <- data.frame(ID = c("a"),
cardiovasc = c(T),
beta_blockers = c(F),
antibiotics = c(T),
stringsAsFactors=FALSE)
As you can tell, even though df$antibiotics only had 1 TRUE in the dataset, I'd like that to count as TRUE when the ID has been collapsed into one row.
What I've tried:
Mainly, I've been trying to work off this post, and while I feel I'm close, I nevertheless get an error. Here's my attempt:
df <- df[, lapply(.SD, paste0, collapse=""), by=ID]
Which yields unused argument (by = ID). I've tried another approach from the same post, but that's even messier and requires me to make the data a data.table. I need to keep things as a data.frame.
Any ideas?