Gordon, Alan wrote on 01/24/2012 09:15:31 AM:
> Dear R users,
>
> I'm a new user to R and have a data set consisting of a number of
> variables (in a data frame). I wish to carry out a regression
> analysis of the first variable against all the rest in turn. I have
> used the following code to do this
>
> dd<-read.table("for loop.txt",header=T)
> for (j in 2:5) print(summary(lm(formula = dd[,1] ~ dd[,j])))
>
> However, although it produces all the analyses that I want, the
> output is annotated with dd[,1] and dd[,j] rather than the actual
> variable names. Is there any way around this?
>
> Best wishes,
> Alan.
Try this:
for (j in 2:5) {
model <- paste("lm(formula =", names(dd[1]), "~",
names(dd[j]), ",
data = dd)")
print(summary(eval(parse(text=model))))
}
Jean
[[alternative HTML version deleted]]