打印数据框时如何更改观星者中的列名?
How to change column names in stargazer when printing data frames?
我正在尝试使用 stargazer 包在乳胶中输出数据框。我希望列名称包含乳胶代码,但 stargazer 不允许在数据框名称中包含乳胶代码。我也尝试过使用 column.labels 参数,但是这个参数只用于回归表,而不是用于输出数据帧。这是我尝试过的两种方法。都没有用。
第一种方法 - 尝试更改数据框中变量的名称
代码:
# Creating a data frame
df = data.frame(x = 1:5, y = 6:10)
# Changing names
names(df) = c("$X$", "$Y$\textsuperscript{1}")
# Exporting
stargazer(df, summary = F,
notes = "\textsuperscript{1} This is a note that was supposed to refer to $Y$.")
输出(显然stargazer不识别LaTeX代码):
% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Sat, Oct 29, 2016 - 20:46:22
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{@{\extracolsep{5pt}} ccc}
\[-1.8ex]\hline
\hline \[-1.8ex]
& $X$ & $Y$\textbackslash textsuperscript\{1\} \
\hline \[-1.8ex]
1 & $ & $ \
2 & $ & $ \
3 & $ & $ \
4 & $ & $ \
5 & $ & $ \
\hline \[-1.8ex]
\end{tabular}
\end{table}
第二种方法——尝试使用column.labels参数
代码:
# Creating a data frame
df = data.frame(x = 1:5, y = 6:10)
# Exporting
stargazer(df, summary = F,
column.labels = c("$X$", "$Y$\textsuperscript{1}"),
notes = "\textsuperscript{1} This is a note that was supposed to refer to $Y$.")
输出(观星者直接忽略参数):
% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Sat, Oct 29, 2016 - 20:57:41
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{@{\extracolsep{5pt}} ccc}
\[-1.8ex]\hline
\hline \[-1.8ex]
& x & y \
\hline \[-1.8ex]
1 & $ & $ \
2 & $ & $ \
3 & $ & $ \
4 & $ & $ \
5 & $ & $ \
\hline \[-1.8ex]
\multicolumn{3}{l}{\textsuperscript{1} This is a note that was supposed to refer to $Y$.} \
\end{tabular}
\end{table}
有点 hacky,但这里的主要思想是 gsub
使用我们想要的 Latex 代码输出 stargazer
中的相关行:
# Creating a data frame
df = data.frame(x = 1:5, y = 6:10)
out <- capture.output(
stargazer(df, summary = F,
notes = "\textsuperscript{1} This is a note that was supposed to refer to $Y$.")
)
# Changing names
vars = c("x" = "$X$", "y" = "$Y$\\textsuperscript{1}")
cat(sep = "\n",
gsub(paste(names(vars), collapse = " & "), paste(vars, collapse = " & "), out)
)
# \begin{table}[!htbp] \centering
# \caption{}
# \label{}
# \begin{tabular}{@{\extracolsep{5pt}} ccc}
# \[-1.8ex]\hline
# \hline \[-1.8ex]
# & $X$ & $Y$\textsuperscript{1} \
# \hline \[-1.8ex]
# 1 & $ & $ \
# 2 & $ & $ \
# 3 & $ & $ \
# 4 & $ & $ \
# 5 & $ & $ \
# \hline \[-1.8ex]
# \multicolumn{3}{l}{\textsuperscript{1} This is a note that was supposed to refer to $Y$.} \
# \end{tabular}
# \end{table}
当使用参数 align = T
时,尝试
vars = sprintf("\\multicolumn\{1\}\{c\}\{%s\}",
c("$X$", "$Y$\\textsuperscript{1}"))
names(vars) <- sprintf("\\multicolumn\{1\}\{c\}\{%s\}", names(df))
cat(sep = "\n",
gsub(paste(names(vars), collapse = " & "), paste(vars, collapse = " & "), out)
)
你可以这样做,但是运行设置名称之前的代码,你不想输入不相关的名称,
stargazer(GLM.1$finalModel, GLM.2$finalModel, GLM.3$finalModel,
out = "my.GLMs.htm",
title = "Logistic Models", type = "html",
single.row = TRUE, report = "vc*", df = FALSE, header = FALSE, digits=2,
column.labels = c("GLM 1", "GLM 2", "GLM 3"), apply.coef=exp,
p.auto=F, dep.var.caption = "Odds Ratio", intercept.bottom = FALSE,
covariate.labels=c( "Gender", "Marital Status","Age", "Employed", "Education",
"Political Affiliation", "Rural", "Ethnicity", "Region", "Gross Income", "Networth","Networth-HomeEquity", "Liquid Assets"))
我正在尝试使用 stargazer 包在乳胶中输出数据框。我希望列名称包含乳胶代码,但 stargazer 不允许在数据框名称中包含乳胶代码。我也尝试过使用 column.labels 参数,但是这个参数只用于回归表,而不是用于输出数据帧。这是我尝试过的两种方法。都没有用。
第一种方法 - 尝试更改数据框中变量的名称
代码:
# Creating a data frame
df = data.frame(x = 1:5, y = 6:10)
# Changing names
names(df) = c("$X$", "$Y$\textsuperscript{1}")
# Exporting
stargazer(df, summary = F,
notes = "\textsuperscript{1} This is a note that was supposed to refer to $Y$.")
输出(显然stargazer不识别LaTeX代码):
% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Sat, Oct 29, 2016 - 20:46:22
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{@{\extracolsep{5pt}} ccc}
\[-1.8ex]\hline
\hline \[-1.8ex]
& $X$ & $Y$\textbackslash textsuperscript\{1\} \
\hline \[-1.8ex]
1 & $ & $ \
2 & $ & $ \
3 & $ & $ \
4 & $ & $ \
5 & $ & $ \
\hline \[-1.8ex]
\end{tabular}
\end{table}
第二种方法——尝试使用column.labels参数
代码:
# Creating a data frame
df = data.frame(x = 1:5, y = 6:10)
# Exporting
stargazer(df, summary = F,
column.labels = c("$X$", "$Y$\textsuperscript{1}"),
notes = "\textsuperscript{1} This is a note that was supposed to refer to $Y$.")
输出(观星者直接忽略参数):
% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Sat, Oct 29, 2016 - 20:57:41
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{@{\extracolsep{5pt}} ccc}
\[-1.8ex]\hline
\hline \[-1.8ex]
& x & y \
\hline \[-1.8ex]
1 & $ & $ \
2 & $ & $ \
3 & $ & $ \
4 & $ & $ \
5 & $ & $ \
\hline \[-1.8ex]
\multicolumn{3}{l}{\textsuperscript{1} This is a note that was supposed to refer to $Y$.} \
\end{tabular}
\end{table}
有点 hacky,但这里的主要思想是 gsub
使用我们想要的 Latex 代码输出 stargazer
中的相关行:
# Creating a data frame
df = data.frame(x = 1:5, y = 6:10)
out <- capture.output(
stargazer(df, summary = F,
notes = "\textsuperscript{1} This is a note that was supposed to refer to $Y$.")
)
# Changing names
vars = c("x" = "$X$", "y" = "$Y$\\textsuperscript{1}")
cat(sep = "\n",
gsub(paste(names(vars), collapse = " & "), paste(vars, collapse = " & "), out)
)
# \begin{table}[!htbp] \centering
# \caption{}
# \label{}
# \begin{tabular}{@{\extracolsep{5pt}} ccc}
# \[-1.8ex]\hline
# \hline \[-1.8ex]
# & $X$ & $Y$\textsuperscript{1} \
# \hline \[-1.8ex]
# 1 & $ & $ \
# 2 & $ & $ \
# 3 & $ & $ \
# 4 & $ & $ \
# 5 & $ & $ \
# \hline \[-1.8ex]
# \multicolumn{3}{l}{\textsuperscript{1} This is a note that was supposed to refer to $Y$.} \
# \end{tabular}
# \end{table}
当使用参数 align = T
时,尝试
vars = sprintf("\\multicolumn\{1\}\{c\}\{%s\}",
c("$X$", "$Y$\\textsuperscript{1}"))
names(vars) <- sprintf("\\multicolumn\{1\}\{c\}\{%s\}", names(df))
cat(sep = "\n",
gsub(paste(names(vars), collapse = " & "), paste(vars, collapse = " & "), out)
)
你可以这样做,但是运行设置名称之前的代码,你不想输入不相关的名称,
stargazer(GLM.1$finalModel, GLM.2$finalModel, GLM.3$finalModel,
out = "my.GLMs.htm",
title = "Logistic Models", type = "html",
single.row = TRUE, report = "vc*", df = FALSE, header = FALSE, digits=2,
column.labels = c("GLM 1", "GLM 2", "GLM 3"), apply.coef=exp,
p.auto=F, dep.var.caption = "Odds Ratio", intercept.bottom = FALSE,
covariate.labels=c( "Gender", "Marital Status","Age", "Employed", "Education",
"Political Affiliation", "Rural", "Ethnicity", "Region", "Gross Income", "Networth","Networth-HomeEquity", "Liquid Assets"))