Skip to content

Instantly share code, notes, and snippets.

@stevenworthington
Created April 4, 2012 15:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenworthington/2302888 to your computer and use it in GitHub Desktop.
Save stevenworthington/2302888 to your computer and use it in GitHub Desktop.
concatenate variables into a formula
# example of how to concatenate lots of variables into a formula without typing them out
# example from the High School and Beyond data set
hsb_df <- read.table("http://www.ats.ucla.edu/stat/R/notes/hs0.csv", header = TRUE, sep = ",")
# create the formula using variables from columns 4:8 and 10:11 as predictors
hsb_form <- formula( paste(c("math ~ 1", colnames(hsb_df[, c(4:8, 10:11)])), collapse = " + ") )
# fit the model
fit1 <- lm(hsb_form, data = hsb_df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment