star_rhs_order: Manipulate the order of right-hand side variables using regex

View source: R/star_rhs_order.R

star_rhs_orderR Documentation

Manipulate the order of right-hand side variables using regex

Description

Use regular expressions to manipulate the right hand side variables in in a stargazer regrssion or table. This function is compatible works with stargazer output of type latex and text.

Usage

star_rhs_order(star, pattern, reg = TRUE)

Arguments

star

the stargazer table

pattern

a vector of regular expressions which the order with which the right-hand-side variables will be used

reg

(logical) set to TRUE for a regression; defaults to TRUE

Value

character vector with stargazer output with the updated order

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)
star.out <- star_rhs_order(star.out, c("cyl", "hp", "wt", "Constant"))
print(star.out)
##text example
star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, type = "text")
print(star.out)
star.out <- star_rhs_order(star.out, c("cyl", "hp", "wt", "Constant"))
print(star.out)

## -- 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_order(star.out.summ, c("hp", "mpg", "wt"), reg = FALSE)
print(star.out.summ)
##text example
star.out.summ <- stargazer(mtcars[,c("mpg", "hp", "wt")], type = "text")
print(star.out.summ)
star.out.summ <- star_rhs_order(star.out.summ, c("hp", "mpg", "wt"), reg = FALSE)
print(star.out.summ)

## -- Keeping a subset of all variables -- ##
##As star_rhs_order() matches based on regular expressions,
##it can also be used to show only a subset of all variables

##Latex example
star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, type = "latex")
print(star.out)
##Only keep hp
star.out <- star_rhs_order(star.out, c("hp"))
print(star.out)

##text example
star.out <- stargazer(mod.mtcars.1, mod.mtcars.2, type = "text")
print(star.out)
##Only keep hp
star.out <- star_rhs_order(star.out, c("hp"))
print(star.out)


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