I have a data frame that is effectively stored regression results, with relevant values as columns and variables as rows (each stored in names). It looks something like this:
a <- c(3, 0.5, 0.010, 4, 0.3, 0.271)
b <- c(10, 0.9, 0.021, 8, 0.5, 0.063)
temp <- data.frame(rbind(b, a))
colnames(temp) <- c("Coef", "SE", "p-value", "Coef", "SE", "p-value")
rownames(temp) <- c("alpha", "bravo")
Using stargazer
, I'd like to create an HTML table that presents this information and allows me to identify the first three columns as being results for Treatment 1 and the second three as results for Treatment 2. The table would look something like this:
table <- stargazer(temp, type="html", summary=F)
The problem I have is that I cannot figure out how to add those identifiers for T1 and T2. I first tried column.labels
in conjunction with column.separate
but have since learned that those only work when using stargazer for actual regression output rather than just a plain data frame. Something like dep.var.caption
would actually be preferable (I like the line separating this identifier and the column heads), but I think that too only works with pure regression output, and even still I can't find a way to do separate ones that each span multiple columns.
Any ideas of how I could make this work, or find a workaround?
I don't now much about stargazer, but strongly recommend using kableExtra when working with HTML/Latex tables in R.
Solution using kableExtra
:
Generate example data:
a <- c(3, 0.5, 0.010, 4, 0.3, 0.271)
b <- c(10, 0.9, 0.021, 8, 0.5, 0.063)
temp <- data.frame(rbind(b, a))
colnames(temp) <- c("Coef", "SE", "p-value", "Coef", "SE", "p-value")
temp
Coef SE p-value Coef SE p-value
b 10 0.9 0.021 8 0.5 0.063
a 3 0.5 0.010 4 0.3 0.271
Generate HTML table:
library(knitr)
library(kableExtra)
kable(temp, format = "html",
row.names = FALSE,
caption = "Way better than stargazer") %>%
kable_styling(bootstrap_options = "striped",
full_width = TRUE) %>%
add_header_above(c("alpha" = 3, "bravo" = 3))