Skip to content

Instantly share code, notes, and snippets.

@docsteveharris
Created May 23, 2016 11:10
Show Gist options
  • Save docsteveharris/b04940996d72393e15d461a52cd7633c to your computer and use it in GitHub Desktop.
Save docsteveharris/b04940996d72393e15d461a52cd7633c to your computer and use it in GitHub Desktop.
Snippet to check median number of drugs administered per drug per site across all patients
# calculate average number of drug doses per patient
col.names.drugs <- names(Filter(function(y) (!is.null(y) && y=="Drugs") , map(dict, "Classification1")))
tdt <- copy(data2d.wide)
tdt[, id:= paste0(site, "_", episode_id)]
setkey(tdt, id)
dim(tdt)
names(tdt)
col.names <- col.names.drugs[col.names.drugs %in% names(tdt)]
tdt[1:10,print(.SD), by=id, .SDcols=col.names]
drug.doses <- tdt[,lapply(.SD, function(x) sum(ifelse(x=="NA",as.integer(NA),1L), na.rm=TRUE)), by=.(site, id), .SDcols=col.names]
relabel_cols(drug.doses, "NHICcode", "dataItem", dict=dict)
str(drug.doses)
head(drug.doses)
x <- drug.doses[,lapply(.SD, quantile, probs=c(0.5), na.rm=T), by=.(site), .SDcols=!c("id")]
y <- t(data.frame(x)[,2:64])
colnames(y) <- x[,site]
y
dim(x)
x[,.(3:64)]
t(x[,c(1,2)])
x <- drug.doses[,lapply(.SD, quantile, probs=c(0.25,0.5,0.75), na.rm=T), by=.(site), .SDcols=!c("id")]
x <- drug.doses[,lapply(.SD, summary, na.rm=T), by=.(site), .SDcols=!c("id")]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment