[R] First Variable in lm

Gabor Grothendieck ggrothendieck at myway.com
Wed Mar 24 18:51:20 CET 2004


Christian Hoffmann <christian.hoffmann <at> wsl.ch> writes:

> 
> Hi all,
> 
> I just cannot think of how to do it:
> I want to take the first variable (column) of a data frame and regress 
> it against all other variables.
> 
> bla <- function (dat) {
>    reg <- lm(whateverthefirstofthevariablenamesis ~., data=dat)
>    return(reg)
> }

Andy has already given a particularly concise solution but if your
variable is not in first position then you could rearrange the 
order of the variables to allow his solution or use this which works 
for any specified position of the dependent variable:

data(longley)
lm( longley[,7] ~. , data = longley[,-7] )




More information about the R-help mailing list