Reputation: 43
New to R, how do you perform a regression in R without listing the variable name, but instead the column number? I have many regression I want to estimate on a loop, I'm trying to do this,
stepreg<-with(newdata, lm(OutputChg~Furrow))
And I want to call Furrow
by its column # instead of title. Is this possible? As always, any help is greatly appreciated, thank you!
Upvotes: 0
Views: 449
Reputation: 70643
You could construct a formula inside each loop and use it in the regression, something along the lines of
allnames <- names(iris)[-5] # find names to which you wish to regress
for (name in allnames) {
myformula <- formula(sprintf("%s ~ Species", name))
print(summary(lm(myformula, data = iris)))
}
Upvotes: 1