[R] metaprogramming with lm

Bill.Venables at csiro.au Bill.Venables at csiro.au
Thu Nov 13 01:56:32 CET 2008

Two possible ways around this are

1. If the x's are *all* the other variables in your data frame you can use a dot:

fm <- lm(y ~ ., data = myData)

2. Here is another idea

> as.formula(paste("y~", paste("x",1:10, sep="", collapse="+")))
y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10

(You bore easily!)

Bill Venables

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of June Kim
Sent: Thursday, 13 November 2008 10:27 AM
To: r-help at r-project.org
Subject: [R] metaprogramming with lm


Say I want to make a multiple regression model with the following expression:

lm(y~x1 + x2 + x3 + ... + x_n,data=mydata)

It gets boring to type in the whole independent variables, in this
case x_i. Is there any simple way to do the metaprogramming for this?
(There are different cases where the names of the independent
variables might sometimes have apparent patterns or not)

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list