HelponR
2006-Oct-08 00:17 UTC
[R] can lm() automatically take in the independent variables without knowing the names in advance
Hello! I am trying to use lm to do a simple regression but on a batch of different files. Each file has different column names. I know the first column is the dependent variable and all the rest are explanatory variables. The column names are in the first line of the file. But it seems lm() requires I know the variable names in advance? Is there a way to dynamically read in variable names and send to lm()? Many many thanks!!! Urania [[alternative HTML version deleted]]
Leeds, Mark (IED)
2006-Oct-08 00:28 UTC
[R] can lm() automatically take in the independent variableswithout knowing the names in advance
someone may know how to do what you asked but a get around is to not
read the header in the file and assign
your own variable names to each column. This way you have control and
you don't worry about doing anything dynamically.
This is pretty easy if you are using read.table and probably also
possible with other types of in "read in file" functions.
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of HelponR
Sent: Saturday, October 07, 2006 8:18 PM
To: r-help
Subject: [R] can lm() automatically take in the independent
variableswithout knowing the names in advance
Hello!
I am trying to use lm to do a simple regression but on a batch of
different files.
Each file has different column names.
I know the first column is the dependent variable and all the rest are
explanatory variables.
The column names are in the first line of the file.
But it seems lm() requires I know the variable names in advance?
Is there a way to dynamically read in variable names and send to lm()?
Many many thanks!!!
Urania
[[alternative HTML version deleted]]
______________________________________________
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.
--------------------------------------------------------
This is not an offer (or solicitation of an offer) to buy/se...{{dropped}}
Gabor Grothendieck
2006-Oct-08 00:38 UTC
[R] can lm() automatically take in the independent variables without knowing the names in advance
Try this:
run.lm <- function(DF, response = names(DF)[1], fo = y~.) {
fo[[2]] <- as.name(response)
eval(substitute(lm(fo, DF)))
}
# test
run.lm(iris)
run.lm(iris, "Sepal.Width")
Another possibility is to rename the first column:
On 10/7/06, HelponR <suncertain at gmail.com>
wrote:> Hello!
>
> I am trying to use lm to do a simple regression but on a batch of
> different files.
>
> Each file has different column names.
>
> I know the first column is the dependent variable and all the rest are
> explanatory variables.
>
> The column names are in the first line of the file.
>
> But it seems lm() requires I know the variable names in advance?
>
> Is there a way to dynamically read in variable names and send to lm()?
>
> Many many thanks!!!
>
>
> Urania
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
Bjørn-Helge Mevik
2006-Oct-09 07:58 UTC
[R] can lm() automatically take in the independent variables without knowing the names in advance
HelponR wrote:> I am trying to use lm to do a simple regression but on a batch of > different files. > > Each file has different column names. > > I know the first column is the dependent variable and all the rest are > explanatory variables.I believe> lm(data = thedataframe)(i.e. with no formula!) will use the first column as response and the rest as predictors. -- Bj?rn-Helge Mevik