stargazer summary.stat重新标记列标题

时间:2016-05-24 10:48:19

标签: r labels summary stargazer

是否可以在stargazer中重新标记summary.stats的列标题?默认标签似乎忽略了我的首选标签。提前谢谢!

library(stargazer)
stargazer(attitude, 
           column.labels = c("Obs", "P25", "P50", "P75"),
           summary.stat = c("n", "p25", "median", "P75")
)

1 个答案:

答案 0 :(得分:0)

不幸的是,column.labels命令仅适用于使用stargazer创建的回归表。但是,由于观星者也可以直接输出数据帧,因此您可以创建自己的数据框,其中包含所需的摘要统计信息以及您希望它们具有的名称,如下所示:

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"))