1

I want separate two columns in a stargazer regression table. So far I have not found a suitable solution. Therefore I write here my question.

Here is the example code to create a stargazer table with 2 columns:

mod <- lm(data=iris,Sepal.Length~Species)
mod1 <- lm(data=iris,Sepal.Length~Petal.Width+Species)

stargazer(mod,mod1, type = "latex")

RMarkdown gives me this output:

enter image description here

But I would like to have both columns separated with a line: enter image description here

Can anyone help me with this issue?

I assume that you have to use latex code to change the output. I have not found any possibilities in the Stargazer options.

Thanks in advance!

TobKel
  • 1,293
  • 8
  • 20
  • 1
    In case you won't be able to do want you want natively with Stargazer one option is editing the resulting LaTeX code from within R: https://stackoverflow.com/questions/38886442/formatting-notes-in-rs-stargazer-tables – s_baldur Feb 15 '21 at 14:19

1 Answers1

1

A proposition:

mod <- lm(data=iris,Sepal.Length~Species)
mod1 <- lm(data=iris,Sepal.Length~Petal.Width+Species)
mod1_sg <- capture.output(stargazer::stargazer(mod, mod1, type = "text"))
library(stringr)
mod1_sg[6:25] <- paste(str_sub(mod1_sg[6:25],1,44), str_sub(mod1_sg[6:25],46,68), sep="|")
mod1_df <- setNames(as.data.frame(noquote(mod1_sg)[-1]),"")
print(mod1_df, row.names=FALSE)
#>                                                                      
#>  ====================================================================
#>                                    Dependent variable:               
#>                      ------------------------------------------------
#>                                        Sepal.Length                  
#>                                (1)           |          (2)          
#>  --------------------------------------------|-----------------------
#>  Petal.Width                                 |       0.917***        
#>                                              |        (0.194)        
#>                                              |                       
#>  Speciesversicolor           0.930***        |        -0.060         
#>                              (0.103)         |        (0.230)        
#>                                              |                       
#>  Speciesvirginica            1.582***        |        -0.050         
#>                              (0.103)         |        (0.358)        
#>                                              |                       
#>  Constant                    5.006***        |       4.780***        
#>                              (0.073)         |        (0.083)        
#>                                              |                       
#>  --------------------------------------------|-----------------------
#>  Observations                  150           |          150          
#>  R2                           0.619          |         0.669         
#>  Adjusted R2                  0.614          |         0.663         
#>  Residual Std. Error     0.515 (df = 147)    |   0.481 (df = 146)    
#>  F Statistic         119.265*** (df = 2; 147)|98.525*** (df = 3; 146)
#>  ====================================================================
#>  Note:                                    *p<0.1; **p<0.05; ***p<0.01

# Created on 2021-02-15 by the reprex package (v0.3.0.9001)

UPDATE

For LaTeX output:

mod <- lm(data=iris,Sepal.Length~Species)
mod1 <- lm(data=iris,Sepal.Length~Petal.Width+Species)
mod1_sg <- capture.output(stargazer::stargazer(mod, mod1, type = "latex"))
mod1_sg <- sub("lcc", "lc|c", mod1_sg)
writeLines(mod1_sg)

enter image description here

Regards,

barboulotte
  • 395
  • 2
  • 8