Package 'glueformula'

Title: String interpolation to build regression formulas
Description: Has the main function gl that is a variant of glue. It is useful for building regression formulas given vectors of variable names.
Authors: Sebastian Kranz
Maintainer: Sebastian Kranz <[email protected]>
License: GPL >= 2.0
Version: 0.1.1
Built: 2024-11-10 06:15:21 UTC
Source: https://github.com/skranz/glueformula

Help Index


String interpolation to build regression formulas from vectors with variable names.

Description

Insert vectors with variable names into { } placeholder in a formula and collapse vectors with +.

Usage

gf(
  form,
  values = list(),
  enclos = parent.frame(),
  as.formula = TRUE,
  form.env = parent.frame(),
  collapse = " + "
)

Arguments

form

A formula or string of a formula. E.g. y~x+{controls} where controls is a character vector with variables that will be inserted.

values

A named list containing the vectors of variable names. If empty the enclosing environment will be searched.

enclos

the enclosing environment in which the vectors with variable names will be taken if not provided by values. By default the calling enviornment.

as.formula

if TRUE (default) return a formula object, otherwise a string.

form.env

The environment assigned to the returned formula. By default the calling environment.

collapse

how shall variables in a vector be collapsed. Default " # ".

Details

An adaption of the glue function specialized to build regression formulas from variable names stored in vectors.

Examples

# Assume we want to estimate a demand function
# using ivreg

contr = c("x1","x2","x3") # exogenous control variables
instr = c(contr, "z1","z2") # instruments

# Replace {contr} and {instr} in formula
gf(q ~ p + {contr} | {instr})

# You could also provide the formula as string
gf("q ~ p + {contr} | {instr}")

Creates a simple snippet code to generate a string vector of all column names of a data frame

Description

The if the data frame has 3 columns named "x", "y" and "name with space" the function cats and on windows also copies to your clipboard the following code:

Usage

varnames_snippet(x)

Arguments

x

A data frame or other objects with names.

Details

c("x","y","`name with space`")

This can be helpful to specify vectors of control variables when using gf. If there are many variables it is just easier to remove not-used variables from the created code snippet than to write down manually all used variables.

Examples

df = data.frame(x=1,y=5,z=4,`a b`=4)
varnames_snippet(df)