Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stargazer summary.stat relabel column headings

Is it possible to relabel column headings for summary.stats in stargazer? The default labels seem to ignore my preferred labels. Thanks in advance!

library(stargazer)
stargazer(attitude, 
           column.labels = c("Obs", "P25", "P50", "P75"),
           summary.stat = c("n", "p25", "median", "P75")
)
like image 653
overeducatedpoverty Avatar asked Oct 19 '25 13:10

overeducatedpoverty


1 Answers

Unfortunately, the column.labels command only applies to regression tables created with stargazer. However, since stargazer can also directly output dataframes, you can create your own dataframe containing the desired summary statistics with the names you want them to have, like so:

library(stargazer)
# create data frame first, set nrow to number of your variables
dfdescriptives <- data.frame(matrix(nrow = 3, ncol = 0))

# specify the variables you want to summarize here
vars <- attitude[, c("var1","var5","id")]

# assign inteligible variable names as rownames
row.names(dfdescriptives) <- c("Variable 1", "Variable 5", "ID Variable")

# get number of observations for each variable
dfdescriptives$Obs <- apply(vars, 2, function(x) sum(complete.cases(x)))
# get 25th percentile for each variable
dfdescriptives$P25 <- apply(vars, 2, function(x) summary(x)[[2]])
# get median for each variable
dfdescriptives$P50 <- apply(vars, 2, function(x) summary(x)[[3]])
# get 75th percentile for each variable
dfdescriptives$P75 <- apply(vars, 2, function(x) summary(x)[[5]])

# output dataframe directly w/o summary
stargazer(dfdescfull, summary = FALSE, header = FALSE, 
      title = "Descriptive Statistics as I would like to call them.",
      notes = c("Source: stackoverflow.com"))
like image 155
fmerhout Avatar answered Oct 22 '25 03:10

fmerhout