star_rhs_names: To replace the names of the RHS variables in 'stargazer'...

View source: R/star_rhs_names.R

star_rhs_namesR Documentation

To replace the names of the RHS variables in stargazer output

Description

To replace the names of the RHS variables in stargazer. The function also allows for an optional second line.

Usage

star_rhs_names(star, pattern, line1, line2 = NULL)

Arguments

star

the stargazer output

pattern

the regular expression pattern

line1

the variable name in the first line

line2

the optional variable name in the second line. The line2 argument takes advantage of the notion that the standard error (for example) of a regression is printed on the line below the coefficient. NOTE: Text in line2 will be inserted directly.

Details

Note: the pattern and line1 arguments must have the same length. If line2 is not NULL, it must have the same length as pattern and line1. Also, line2 will be inserted into star directly.

Value

a character vector with the updated stargazer output

Examples

## -- Regression example -- ##
library(stargazer)
data(mtcars)
mod.mtcars.1 <- lm(mpg ~ hp + wt, mtcars)
mod.mtcars.2 <- lm(mpg ~ hp + wt + cyl, mtcars)

##Latex example
star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, type = "latex")
print(star.out)
##Update the names hp and wt using one line only
star.out.1 <- star_rhs_names(star.out,
                             pattern = c("hp", "wt"),
                             line1 = c("horsepower", "weight")
                            )
print(star.out.1)
##Sometimes variables can have really long names and
##would be best written on 2 lines
star.out.1 <- star_rhs_names(star.out,
                             pattern = c("hp", "wt"),
                             line1 = c("horse-", "weight"),
                             line2 = c("power", "")
                            )
print(star.out.1)

##Text example
##Note that star_rhs_names() will NOT adjust the
##length of the character elements if the variable names in line1
##or line 2 are longer than those that they are replacing
star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, type = "text")
print(star.out)
##Update the names hp and wt using oneline
star.out.1 <- star_rhs_names(star.out,
                             pattern = c("hp", "wt"),
                             line1 = c("horsepower", "weight")
                            )
print(star.out.1)
##Sometimes variables can have really long names and
##would be best written on 2 lines
star.out.1 <- star_rhs_names(star.out,
                             pattern = c("hp", "wt"),
                             line1 = c("horse-", "weight"),
                             line2 = c("power", "")
                            )
print(star.out.1)


## -- Summary stats example -- ##
##latex example
star.out.summ <- stargazer(mtcars[,c("mpg", "hp", "wt")], type = "latex")
print(star.out.summ)
star.out.summ <- star_rhs_names(star.out.summ,
                                pattern = c("hp", "wt"),
                                line1 = c("horsepower", "weight")
                               )
print(star.out.summ)
##text example
star.out.summ <- stargazer(mtcars[,c("mpg", "hp", "wt")], type = "text")
star.out.summ <- star_rhs_names(star.out.summ,
                                pattern = c("hp", "wt"),
                                line1 = c("horsepower", "weight")
                               )
print(star.out.summ)

ChandlerLutz/starpolishr documentation built on Feb. 19, 2025, 2:22 a.m.