# NOT RUN {
n <- 400
set.seed(1)
d <- data.frame(country=factor(sample(c('US','Canada','Mexico'), n, TRUE)),
sex=factor(sample(c('Female','Male'), n, TRUE)),
age=rnorm(n, 50, 10),
sbp=rnorm(n, 120, 8))
d <- upData(d,
preghx=ifelse(sex=='Female', sample(c('No','Yes'), n, TRUE), NA),
labels=c(sbp='Systolic BP', age='Age', preghx='Pregnancy History'),
units=c(sbp='mmHg', age='years'))
contents(d)
require(tables)
invisible(booktabs()) # use booktabs LaTeX style for tabular
g <- function(x) {
x <- x[!is.na(x)]
if(length(x) == 0) return('')
paste(latexNumeric(nFm(mean(x), 3, 1)),
' \hfill{\smaller[2](', length(x), ')}', sep='')
}
tab <- tabulr((age + Heading('Females')*(sex == 'Female')*sbp)*
Heading()*g + (age + sbp)*Heading()*trio ~
Heading()*country*Heading()*sex, data=d)
# Formula after interpretation by tabulr:
# (Heading('Age\hfill {\smaller[2] years}') * age + Heading("Females")
# * (sex == "Female") * Heading('Systolic BP {\smaller[2] mmHg}') * sbp)
# * Heading() * g + (age + sbp) * Heading() * table_trio ~ Heading()
# * country * Heading() * sex
cat('\begin{landscape}\n')
cat('\begin{minipage}{\textwidth}\n')
cat('\keytrio\n')
latex(tab)
cat('\end{minipage}\end{landscape}\n')
getHdata(pbc)
pbc <- upData(pbc, moveUnits=TRUE)
# Convert to character to prevent tabular from stratifying
for(x in c('sex', 'stage', 'spiders')) {
pbc[[x]] <- as.character(pbc[[x]])
label(pbc[[x]]) <- paste(toupper(substring(x, 1, 1)), substring(x, 2), sep='')
}
table_options(pn=TRUE, showfreq='all')
tab <- tabulr((bili + albumin + protime + age) *
Heading()*trio +
(sex + stage + spiders)*Heading()*freq ~ drug, data=pbc)
latex(tab)
# }
Run the code above in your browser using DataLab