r - Fine tuning table output of function 'univariateTable' -


i've found function univariatetable extremely helpful handling larger data nice, clean table output. there couple of things still need manually after table exported in csv, , rather in r automate process , avoid human errors.

here example code table output export csv

value<-cbind(c(rnorm(103.251,503.24,90),rnorm(103.251,823.24,120))) genotype<-cbind(c(rep("a",100),rep("b",100))) gender<-rep(c("m","f","f","f"),50) df<-cbind(value,genotype,gender) df<-as.data.frame(df) colnames(df)<-c("value","genotype","gender") df$value<-as.numeric(as.character(df$value)) library(publish) summary(univariatetable(gender ~ q(value) + genotype, data=df)) 

the 2 problems have these:

  1. is there way round numbers in table in way similar this: round(99.73)

  2. is there way substitute , - in interquartile range output in way similar this: gsub(", ","-","[503.7, 793.3]") , , instead of median [iqr] have put out median [iqr]

again, these manually after exporting tables, larger tables more convenient automate process.

univariatetable has digits argument can use rounding. modify formatting, can inspect list returned univariatetable figure out find values need changed.

your example data threw error, i've modified make run , cleaned code bit.

# devtools::install_github("tagteam/publish") library(publish)  value <- c(rnorm(90, 103.251,503.24),rnorm(110, 103.251,823.24)) genotype <- rep(c("a","b"), each=100) gender <- rep(c("m","f","f","f"),50) df <- data.frame(value,genotype,gender) 

the digits argument univariatetable can used rounding (see ?univariatetable information on function).

tab = univariatetable(gender ~ q(value) + genotype, data=df, digits=0) 

to change commas hyphens, need see values stored in list returned univariatetable. run str(tab), shows structure of list. note heading values in table they're stored in tab$summary.groups$value , tab$summary.totals$value, we'll edit those:

tab$summary.groups$value = gsub(", ", " - ", tab$summary.groups$value) tab$summary.totals$value = gsub(", ", " - ", tab$summary.totals$value)  tab    variable        level gender = f (n=150) gender = m (n=50)   total (n=200) p-value 1    value median [iqr]    -6 [-481 - 424]  203 [-167 - 544] 80 [-433 - 458]   0.118 2 genotype                       75 (50)           25 (50)        100 (50)         3                     b            75 (50)           25 (50)        100 (50)   1.000 

Comments