首页 > 解决方案 > 如何将下标添加到观星表协变量标签?

问题描述

在下面的协变量标签中,我想在下标中包含 P、M 和 C。这是我的代码:

   stargazer(DD1_PT, DD1_MT, DD1_CT,
          type = "text", 
          title = "Table 1", 
          out = "table1.html", 
          dep.var.labels.include = FALSE, 
          column.labels = c("Presidential", "Midterm", "Constitutional"), 
          model.numbers = FALSE, 
          order=c(4, 1, 8, 2, 9, 3, 10, 5, 6, 7),   
          covariate.labels=c("TimeP (1 = 2016/0 = 2012)","TimeP*Vote center", "TimeM (1 = 2018/0 
          = 2010)", "TimeM*Vote center", "TimeC (1 = 2017/0 = 2009)", "TimeC*Vote center"), 
          no.space=TRUE, 
          omit.stat=c("LL","ser","f"), 
          notes = c("Standard errors given in parentheses ", "Population scaled to 1,000,000 and 
          income scaled to 10,000"), 
          notes.align = "r"))

关于我如何做到这一点的任何想法?

谢谢!

标签: labelsubscriptstargazer

解决方案


您的代码具有type = "text". 在纯文本中制作下标字符会很困难。

如果您将输出设置为html,那么您可以使用stringr::str_replace()在 HTML 中放入下标:<sub> <\sub>

对于可重现的示例,让我们使用mtcars

想象一下,我们想让“Observations”的最后一个字母在表格中下标。

图书馆(观星者)
图书馆(tidyverse)

fit1 <- lm(mpg ~ cyl, 数据 = mtcars)
output <- stargazer(fit1, type = "HTML") # 给你输出

# 用下标的 html 替换输出
输出<-str_replace(输出,“观察”,“观察<sub>s</sub>”)

write_lines(输出,“subscript_example.html”)

它给出了所需的下标,如下所示:

在此处输入图像描述

作为最后一点,我已经读到最好使用编辑 HTMLxml2而不是str_replace(). 也许其他人可以提供帮助。上述解决方案对我有用。


推荐阅读