star_lhs_names: To replace the names of the LHS variables in 'stargazer'...

View source: R/star_lhs_names.R

star_lhs_namesR Documentation

To replace the names of the LHS variables in stargazer output

Description

To replace the names of the RFHS variables in stargazer. The function also allows for optional second and third lines

Usage

star_lhs_names(
  star,
  pattern,
  line1,
  line2 = NULL,
  line3 = NULL,
  multicol = 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

line3

the optional variable name in teh third line

multicol

if not NULL, multicolumn labels will be used. Options are "l" for left, "c" for center, and "r" for right. A character vector can be used for different alignments across different columns. If a single character is used, that alignment will be used for all elements in pattern. If a latex multicol is already applied by stargazer, multicol will be ignored for that variable. This argument is only valid for latex output.

Details

Note: the pattern and line1 arguments must have the same length. If line2 and line3 are not NULL, they must have the same length as pattern and line1

Value

character vector with stargazer output with the updated LHS variable names

Examples

## -- Regression Example -- ##
library(stargazer)
data(mtcars)
mod.mtcars.1 <- lm(mpg ~ hp + wt, mtcars)
mod.mtcars.2 <- lm(mpg ~ hp + wt + cyl, mtcars)
mod.mtcars.3 <- lm(hp ~ wt + cyl, mtcars)
##latex example
star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, mod.mtcars.3,
                      type = "latex")
print(star.out)
##update the LHS variable names using one line only
star.out.1 <- star_lhs_names(star.out,
                             pattern = c("mpg", "hp"),
                             line1 = c("miles per gallon", "horsepower")
                            )
print(star.out.1)
##Sometimes variables can have really long names and
##would be best written on 2 lines
star.out.2 <- star_lhs_names(star.out,
                             pattern = c("mpg", "hp"),
                             line1 = c("miles per", "horse-"),
                             line2 = c("gallon", "power")
                            )
print(star.out.2)
##For this example, three lines might be even better
star.out.3 <- star_lhs_names(star.out,
                             pattern = c("mpg", "hp"),
                             line1 = c("miles", "horse-"),
                             line2 = c("per", "power"),
                             line3 = c("gallon", "")
                            )
print(star.out.3)

##For Multicolumn centered columns. Using multicolumn
##for the column labels can be useful for siunitx
##columns or if the column labels have a different alignment
##than the rest of the table
star.out.4 <- star_lhs_names(star.out,
                             pattern = c("mpg", "hp"),
                             line1 = c("miles", "horse-"),
                             line2 = c("per", "power"),
                             line3 = c("gallon", ""),
                             multicol = c("c")
                            )
print(star.out.4)


##Text Examples
##Note that star_lhs_names() will NOT adjust the
##length of the character elements if the variable names in line1,
##line 2, or line3 are longer than those that they are replacing
star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, mod.mtcars.3,
                      type = "text")
print(star.out)
##update the LHS variable names using one line only
star.out.1 <- star_lhs_names(star.out,
                             pattern = c("mpg", "hp"),
                             line1 = c("miles per gallon", "horsepower")
                            )
print(star.out.1)
##Sometimes variables can have really long names and
##would be best written on 2 lines
star.out.2 <- star_lhs_names(star.out,
                             pattern = c("mpg", "hp"),
                             line1 = c("miles per", "horse-"),
                             line2 = c("gallon", "power")
                            )
print(star.out.2)
##For this example, three lines might be even better
star.out.3 <- star_lhs_names(star.out,
                             pattern = c("mpg", "hp"),
                             line1 = c("miles", "horse-"),
                             line2 = c("per", "power"),
                             line3 = c("gallon", "")
                            )
print(star.out.3)

ChandlerLutz/starpolishr documentation built on April 16, 2023, 8:55 a.m.