There are times when we need to write a function that makes changes to a generic data frame that is passed as an argument. Let’s say, for example, that we want to write a function that converts to factor any variable with names starting with a capital letter. There are a few issues involved in this problem, including:
- Obtaining a text version of the name of the dataset (using the
substitute()
function). - Looping over the variable names and checking if they start with a capital letter (comparing with the
LETTERS
vector of constants). - Generating the plain text version of the factor conversion, glueing the dataset and variable names (using
paste()
). - Parsing the plain text version of the code to R code (using
parse()
) and evaluating it (usingeval()
). This evaluation has to be done in the parent environment or we will lose any transformation when we leave the function, which is the reason for theenvir()
specification.