gpt4 book ai didi

r - stargazer summary.stat 重新标记列标题

转载 作者:行者123 更新时间:2023-12-04 17:59:23 27 4
gpt4 key购买 nike

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

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

最佳答案

不幸的是,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"))

关于r - stargazer summary.stat 重新标记列标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37411380/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com