# 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 mean BMI in control group vs. treatment group
meanstable1 <- tabmeans(x = d$group, y = d$bmi, xlevels = groups, yname = "BMI")
# Compare mean BMI by race and include sample size
meanstable2 <- tabmeans(x = d$race, y = d$bmi, xlevels = races, yname = "BMI", n = TRUE)
# Create single table comparing mean BMI and mean age in control vs. treatment group
meanstable3 <- rbind(tabmeans(x = d$group, y = d$bmi, xlevels = groups, yname = "BMI"),
tabmeans(x = d$group, y = d$age, xlevels = groups, yname = "Age"))
# An easier way to make the above table is to call the tabmulti function
meanstable4 <- tabmulti(dataset = d, xvarname = "group", yvarnames = c("bmi", "age"),
xlevels = groups, ynames = c("BMI", "Age"))
# meanstable3 and meanstable4 are equivalent
all(meanstable3 == meanstable4)
# Click on meanstable1, meanstable2, meanstable3, or meanstable4 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