英文:
Rename the add_columns columns in modelsummary and add multiple columns
问题
You can rename the added column within the modelsummary
by specifying the add_columns
argument with a named list. Here's how you can do it:
# Define the data frame with your values and desired column names
new_columns <- data.frame(new_col_1 = c("x", "", "x", "", ""),
new_col_2 = c("", "x", "", "", ""))
# Add the columns with specified names
tab <- modelsummary(list("HP" = reg1, "WT" = reg2, "GEAR" = reg3),
stars = TRUE,
statistic = c("s.e. = {std.error}"),
estimate = "estimate",
fmt = fmt_statistic(p.value = fmt_sprintf("%.2e")),
add_columns = new_columns)
This will add two columns named "new_col_1" and "new_col_2" to the table with the specified values.
To add multiple columns in a different order within the add_columns
argument, you can reorder the columns in your new_columns
data frame accordingly. For example:
# Define the data frame with your values and desired column names in the desired order
new_columns <- data.frame(new_col_2 = c("", "x", "", "", ""),
new_col_1 = c("x", "", "x", "", ""))
# Add the columns with specified names and order
tab <- modelsummary(list("HP" = reg1, "WT" = reg2, "GEAR" = reg3),
stars = TRUE,
statistic = c("s.e. = {std.error}"),
estimate = "estimate",
fmt = fmt_statistic(p.value = fmt_sprintf("%.2e")),
add_columns = new_columns)
This will add "new_col_2" after the first regression and "new_col_1" after the second regression, as per your desired order.
英文:
Suppose I have a table summarizing regressions using the modelsummary
package such as the following:
library(fixest)
library(modelsummary)
reg1 <- feols(hp ~ vs + am + qsec | carb, data = mtcars)
reg2 <- feols(wt ~ vs + am + qsec | carb, data = mtcars)
reg3 <- feols(gear ~ vs + am + qsec | carb, data = mtcars)
I plot the summary of the two regressions in a table such as the following:
tab <- modelsummary(list("HP" = reg1, "WT" = reg2, "GEAR" = reg3),stars = TRUE, statistic = c("s.e. = {std.error}"), estimate = "estimate", fmt = fmt_statistic(p.value = fmt_sprintf("%.2e")))
I now will add a column of values to the table using the argument add_columns(data.frame(c("x","","x","","","")))
This works, however it will give me an arbitrary column name. How can I rename this added column within the modelsummary
if I rename the vector/dataframe outside the modelsummary
this would not be ideal as I need to add multiple columns with shared names but different values.
Also, how can one add multiple columns in a different order within the add_columns
argument? For example, if I wanted new_col_1 after the original first regression and another column new_col_2 after the second regression, how can I do this?
答案1
得分: 1
以下是您要翻译的代码部分:
如果位置在现有回归表中的最后一列之后,请在设置属性时将该数字留空。 "modelsummary" 是一个出色的包!
英文:
For anyone who encounters the same question I did, this does the trick:
library(fixest)
library(modelsummary)
reg1 <- feols(hp ~ vs + am + qsec | carb, data = mtcars)
reg2 <- feols(wt ~ vs + am + qsec | carb, data = mtcars)
reg3 <- feols(gear ~ vs + am + qsec | carb, data = mtcars)
#theoretical columns to add
new_col_1 <- c("x","","x","","","")
new_col_2 <- c("x","","","","x","")
#make dataframe
cols <-data.frame(new_col_1,new_col_2)
#add desired names
names(cols) <- c("name1","name2")
#set position attribute as needed
attr(cols, "position") <- c(3,5)
tab <- modelsummary(list("HP" = reg1, "WT" = reg2, "GEAR" = reg3),stars = TRUE, statistic = c("s.e. = {std.error}"), estimate = "estimate", fmt = fmt_statistic(p.value = fmt_sprintf("%.2e")), add_columns = cols)
If the position is after the last column in the existing regression table, just leave that number blank when setting the attribute. modelsummary
is a brilliant package!
答案2
得分: 1
你可以考虑以下方法:
library(modelsummary)
library(fixest)
library(marginaleffects)
library(rvest)
library(dplyr)
library(rmarkdown)
reg1 <- feols(hp ~ vs + am + qsec | carb, data = mtcars)
reg2 <- feols(wt ~ vs + am + qsec | carb, data = mtcars)
reg3 <- feols(gear ~ vs + am + qsec | carb, data = mtcars)
tab <- modelsummary(list("HP" = reg1, "WT" = reg2, "GEAR" = reg3), stars = TRUE)
table <- as.data.frame(read_html(tab[[1]]) %>% html_table())
new_Col <- c(NA, "X", rep(NA, 11))
table <- bind_cols(table[, 1:3], new_Col, table[, 4])
tab <- knitr::kable(table)
你可以将HTML表格转换为数据框。之后,你可以向数据框添加一列,然后将数据框转换回HTML表格。
英文:
You can also consider the following approach :
library(modelsummary)
library(fixest)
library(marginaleffects)
library(rvest)
library(dplyr)
library(rmarkdown)
reg1 <- feols(hp ~ vs + am + qsec | carb, data = mtcars)
reg2 <- feols(wt ~ vs + am + qsec | carb, data = mtcars)
reg3 <- feols(gear ~ vs + am + qsec | carb, data = mtcars)
tab <- modelsummary(list("HP" = reg1, "WT" = reg2, "GEAR" = reg3), stars = TRUE)
table <- as.data.frame(read_html(tab[[1]]) %>% html_table())
new_Col <- c(NA, "X", rep(NA, 11))
table <- bind_cols(table[, 1 : 3], new_Col, table[, 4])
tab <- knitr::kable(table)
You can convert the html table to a data.frame. After, you add a column to the data.frame and then, you convert the data.frame to a html table.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论