[R] basic question re lm()

Simon Blomberg blomsp at ozemail.com.au
Fri Aug 11 02:05:58 CEST 2006


You could look at using lm.fit instead of lm.

Alternatively, you can paste the names of the variables together using 
the following approach. It's a bit baroque, but it works:

form.fn <- function (dframe) {
nms <- names(dframe)
formula(paste(nms[1], "~", paste(nms[2:length(nms)], collapse="+")))
}

Then do:

form <- form.fn(data1)
lm(form, data=data1)

HTH,

Simon.


r user wrote:
> I am using R in a Windows environment.
>
> I have a basic question regarding lm().
>
> I have a dataframe “data1” with ncol=w.
>
> I know that my dependent variable is in column1.
>
> Is there a way to write the regression formula so that
> I can use columns 2 thru w as my independent
> variables?
>
>
>
> e.g. something like:  “ lm(data1[,1] ~ data1[,2:w] ) “
>
> Thanks
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>   


-- 
Simon Blomberg, B.Sc.(Hons.), Ph.D, M.App.Stat.
Centre for Resource and Environmental Studies
The Australian National University
Canberra ACT 0200
Australia
T: +61 2 6125 7800 email: Simon.Blomberg_at_anu.edu.au
F: +61 2 6125 0757
CRICOS Provider # 00120C



More information about the R-help mailing list