# Load in sample dataset d and drop rows with missing values
data(d)
d <- d[complete.cases(d), ]
# Create labels for group and race
groups <- c("Control", "Treatment")
races <- c("White", "Black", "Mexican American", "Other")
# Compare median BMI in control group vs. treatment group
medtable1 <- tabmedians(x = d$group, y = d$bmi, xlevels = groups, yname = "BMI")
# Repeat, but suppress Min-Max from being shown in parentheses
medtable2 <- tabmedians(x = d$group, y = d$bmi, xlevels = groups, yname = "BMI",
parenth = "none")
# Compare median BMI by race and include sample size
medtable3 <- tabmedians(x = d$race, y = d$bmi, xlevels = races, yname = "BMI", n = TRUE)
# Create single table comparing median BMI and median age in control vs. treatment group
medtable4 <- rbind(tabmedians(x = d$group, y = d$bmi, xlevels = groups, yname = "BMI"),
tabmedians(x = d$group, y = d$age, xlevels = groups, yname = "Age"))
# An easier way to make the above table is to call the tabmulti function
medtable5 <- tabmulti(dataset = d, xvarname = "group", yvarnames = c("bmi", "age"),
ymeasures = "median", xlevels = groups, ynames = c("BMI", "Age"))
# medtable4 and medtable5 are equivalent
all(medtable4 == medtable5)
# Click on medtable1, medtable2, medtable3, medtable4, or medtable5 in the Workspace tab
# of RStudio to see the tables that could be copied and pasted into a report or
# manuscript. Alternatively, setting the latex input to TRUE produces tables that can be
# inserted into LaTeX using the xtable package.
Run the code above in your browser using DataLab