星际观测者总结。统计重新标记列标题。

4

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

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

2

不幸的是,column.labels 命令只适用于使用 stargazer 创建的回归表。然而,由于 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"))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接