Created
October 20, 2015 18:47
-
-
Save benneely/4df2462113f8ba93be0b to your computer and use it in GitHub Desktop.
cat cont functions for baseline tables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cat <-function(catvar,colvar,label){ | |
denoms <- tapply(!is.na(catvar),colvar,sum) | |
counts <- tapply(catvar,colvar,sum) | |
percen <- round(100*(counts/denoms),digits=2) | |
totals <- tapply(catvar,rep(1,length(catvar)),sum) | |
totalsden <- tapply(!is.na(catvar),rep(1,length(catvar)),sum) | |
totper <- round(100*(totals/totalsden),digits=2) | |
meatcols <- paste(counts,'/',denoms,'(',percen,'',')',sep="") | |
totcol <- paste(totals,'/',totalsden,'(',totper,'',')',sep="") | |
temp <- table(catvar,colvar) | |
if (sum(as.logical(temp < 15))==0){ | |
a <- chisq.test(temp) | |
} else { | |
a <- fisher.test(temp) | |
} | |
output <- c(label,totcol,meatcols,ifelse(a$p.value<0.0001,'<0.0001',round(a$p.value,3)),a$method) | |
return(output) | |
} | |
cont <- function(contvar,colvar,label,dat){ | |
meds <- tapply(contvar,colvar,median,na.rm=TRUE) | |
Q1 <- tapply(contvar,colvar,quantile,probs=0.25,na.rm=TRUE) | |
Q3 <- tapply(contvar,colvar,quantile,probs=0.75,na.rm=TRUE) | |
totals <- tapply(contvar,rep(1,length(contvar)),median,na.rm=TRUE) | |
totalQ1 <- tapply(contvar,rep(1,length(contvar)),quantile,probs=0.25,na.rm=TRUE) | |
totalQ3 <- tapply(contvar,rep(1,length(contvar)),quantile,probs=0.75,na.rm=TRUE) | |
meatcols <- paste(round(meds,2),' (',round(Q1,2),",",round(Q3,2),")",sep="") | |
totcol <- paste(round(totals,2),' (',round(totalQ1,2),",",round(totalQ3,2),")",sep="") | |
a <- kruskal.test(contvar~colvar,data=dat) | |
output <- c(label,totcol,meatcols,ifelse(a$p.value<0.0001,'<0.0001',round(a$p.value,3)),a$method) | |
return(output) | |
} | |
out1 <- cont(contvar=sanal$b_age,colvar=colvar,label='Baseline Age (visit 4,5, or 6)',dat=sanal) | |
out2 <- cat(catvar=sanal$b_female,colvar=colvar,'Female ()') | |
out3 <- cat(catvar=sanal$b_smoke,colvar=colvar,'Smoking ()') | |
out4 <- cat(catvar=sanal$b_newdiab,colvar=colvar,'Diabetes ()') | |
out5 <- cont(contvar=sanal$b_cbmi,colvar=colvar,'Body Mass Index',sanal) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment