Created
April 4, 2012 15:36
-
-
Save stevenworthington/2302888 to your computer and use it in GitHub Desktop.
concatenate variables into a formula
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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