Search code examples
rfunctionvariablesformula

Create a formula from (very) long list


I have a high-dimensional data frame df with dimensions of 3000 x 80 (a document term matrix). I have a classification function that takes in two arguments: formula and data. For formula, I want it to take all the features (variables) of df automatically. Is there a way to take in a list of all column names to create a formula object?


Solution

  • You could probably do

    reformulate(names(df))
    

    which will produce a one-sided formula with all of the variable names. (It's really not much more than syntactic sugar for as.formula(paste(names(df), collapse="+")).)