juanjedi
juanjedi

Reputation: 160

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?

Upvotes: 0

Views: 818

Answers (2)

Paul
Paul

Reputation: 1

For a formula use:

as.formula(paste( "dependent_var ~ ", paste(names(df), collapse="+"))

Upvotes: -1

Ben Bolker
Ben Bolker

Reputation: 226871

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="+")).)

Upvotes: 4

Related Questions