# Load in sample dataset d and drop rows with missing values
data(d)
d <- d[complete.cases(d), ]
# Create labels for treatment group, sex, and race
groups <- c("Control", "Treatment")
sexes <- c("Female", "Male")
races <- c("White", "Black", "Mexican American", "Other")
# Compare sex distribution by group, with group as column variable
freqtable1 <- tabfreq(x = d$group, y = d$sex, xlevels = groups, ylevels = sexes,
yname = "Sex")
# Same comparison, but compress table to single row and include sample size
freqtable2 <- tabfreq(x = d$group, y = d$sex, xlevels = groups, yname = "Male",
compress = TRUE, n = TRUE)
# Compare sex distribution by race, with race as column variable
freqtable3 <- tabfreq(x = d$race, y = d$sex, xlevels = races, yname = "Sex",
ylevels = sexes)
# Use rbind to create single table comparing sex and race in control vs. treatment group
freqtable4 <- rbind(tabfreq(x = d$group, y = d$sex, xlevels = groups,
ylevels = sexes, yname = "Sex"),
tabfreq(x = d$group, y = d$race, xlevels = groups,
ylevels = races, yname = "Race"))
# An easier way to make the above table is to call the tabmulti function
freqtable5 <- tabmulti(dataset = d, xvarname = "group", yvarnames = c("sex", "race"),
xlevels = groups, ynames = c("Sex", "Race"),
ylevels = list(sexes, races))
# freqtable4 and freqtable5 are equivalent
all(freqtable4 == freqtable5)
# Click on freqtable1, freqtable2, freqtable3, freqtable4, or freqtable5 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