take the colnames of dataframe and collect them into a formula

Use as.formula and paste:

f <- function(x) as.formula(paste("dependent_variable ~", 
                            paste(colnames(x), collapse="+")))

dat <- data.frame(col1=rnorm(10),
                  col2=rnorm(10),
                  col3=rnorm(10))

f(dat)
#> dependent_variable ~ col1 + col2 + col3

Obviously, you just need to change colnames(x) to the vector of column names that you want in the formula.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top