Let's say I have a dataframe of people's names and some categorical variable describing them:
df <- data.frame(name = c("Tom", "Jane", "Will", "Joe", "Sarah", "Mary"),
status = c("friend", "acquaintance", "acquaintance", "stranger",
"stranger", "acquaintance"))
How might I print out a formatted table (to HTML/LaTeX, etc.) where the categories are columns and the names are listed in rows (perhaps in alphabetical order), like:
Ideally I'd like to be able to do as much of the formatting as possible in R, as in packages like stargazer
or huxtable
.
I was thinking a first step might be to reshape it using a tidyr
verb into something that would look like this:
df2 <- data.frame(friend = c("Tom", NA, NA),
acquaintance = c("Jane", "Mary", "Will"),
stranger = c("Joe", "Sarah", NA))
and then try to find a good function for formatting and printing, but I'm not sure if that's the right approach. Thanks!