JoeJackson1234
JoeJackson1234

Reputation: 43

r calling dataframe variable by column number

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

Answers (1)

Roman Luštrik
Roman Luštrik

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

Related Questions