short formula call for many variables when building a model [duplicate]

You can use . as described in the help page for formula. The . stands for “all columns not otherwise in the formula”. lm(output ~ ., data = myData). Alternatively, construct the formula manually with paste. This example is from the as.formula() help page: xnam <- paste(“x”, 1:25, sep=””) (fmla <- as.formula(paste(“y ~ “, paste(xnam, … Read more

Formula with dynamic number of variables

See ?as.formula, e.g.: factors <- c(“factor1”, “factor2”) as.formula(paste(“y~”, paste(factors, collapse=”+”))) # y ~ factor1 + factor2 where factors is a character vector containing the names of the factors you want to use in the model. This you can paste into an lm model, e.g.: set.seed(0) y <- rnorm(100) factor1 <- rep(1:2, each=50) factor2 <- rep(3:4, … Read more