fx2vars: Convert formula object to x,y variables and vice versa

Description Usage Arguments Value Warning Examples

Description

This is a utility function that takes either a formula object, or two vectors of variable names and converts one to the other. Alternatively, if neither of these are supplied, then it will assume the first entry in names is the y value and the rest are the x columns. If names is provided, the function will check that the results are consistent with the data frame from which the names were extracted.

Usage

1
fx2vars(fx = NULL, x = NULL, y = NULL, names = NULL)

Arguments

fx

(optional) a formula object from which to extract x and y variable names.

x

(optional) a character vector of predictor variables.

y

(optional) the name of a dependent variable.

names

(optional) a complete list of names; it will only used to compute variable names if no other variables are supplied, but it will be used to check the validity of the variables if it is given; hence, providing all the names of the variables in the proposed data frame can be an easy and useful check that all is as expected.

Value

Because R does not allow passing by reference, this function reaches into the parent frame and changes the variables that were passed to it. So, for example, if fx was supplied and x and y were not, this function call will change the calling environment such that the variables passed to x and y now store the values encoded by fx. In a similar way, if fx is not specified, but x and y are, then it will set fx in the parent environment to a formula object representing the variables from x and y.

Warning

Note, use this function with caution as it changes your calling parameters! See Value for more information...

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
fx <- x <- y <- NULL
fx2vars(formula('a~b+c'), x, y)
c (fx, x, y)  # fx is still NULL because it wasn't passed to the call

fx <- x <- y <- NULL
fx2vars(fx, c('b','c'), 'a')
c (fx, x, y)  # likewise for a and b

## Not run: 
fx <- x <- y <- NULL
fx2vars(formula('a~b+c'), x, y, names=c('a','b'))
c (fx, x, y)  # x has been trimmed to only contain 'b' as 'c' does not occur in names 
## End(Not run)

henkelstone/NPEL.Classification documentation built on May 17, 2019, 3:42 p.m.