Including a large number of variables in a formula.

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Including a large number of variables in a formula.

R help mailing list-2
Hello,

I have almost 40 variables that I am trying to include in a formula.

I tried to include them using as.formula(), however the variables do not
follow a patter in the name. e.g. These variables are named like: lvacb23
lvacb30 lvacb300  lvacb40 .....  lvacb81.

> lvadd <- paste0("lvacb", 23:81)
> (fmla <- as.formula(paste("lwage ~ ", paste(lvadd, collapse = "+"))))
> fit <- lm(fmla, data = wageszm14)
Error in eval(predvars, data, env) : object 'lvacb24' not found

The variable lvacb24 doesn't exist, because from lvacb23 it jumps to
lvacb30.

Thanks in advance for any help.
--
Rolando Valdez
Facultad de Economía y Relaciones Internacionales
Universidad Autónoma de Baja California

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Including a large number of variables in a formula.

Peter Dalgaard-2
Two ideas:

nn <- names(wageszm14)
lvadd <- nn[grep("^lvacb", nn)]

or

lvadd <- paste0("lvacb", 23:81)
lvadd <- lvadd[lvadd %in% names(wageszm14)]

> On 5 Jun 2019, at 06:46 , Rolando I. Valdez via R-help <[hidden email]> wrote:
>
> Hello,
>
> I have almost 40 variables that I am trying to include in a formula.
>
> I tried to include them using as.formula(), however the variables do not
> follow a patter in the name. e.g. These variables are named like: lvacb23
> lvacb30 lvacb300  lvacb40 .....  lvacb81.
>
>> lvadd <- paste0("lvacb", 23:81)
>> (fmla <- as.formula(paste("lwage ~ ", paste(lvadd, collapse = "+"))))
>> fit <- lm(fmla, data = wageszm14)
> Error in eval(predvars, data, env) : object 'lvacb24' not found
>
> The variable lvacb24 doesn't exist, because from lvacb23 it jumps to
> lvacb30.
>
> Thanks in advance for any help.
> --
> Rolando Valdez
> Facultad de Economía y Relaciones Internacionales
> Universidad Autónoma de Baja California
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Including a large number of variables in a formula.

R help mailing list-2
The first idea works perfect.

Thank you!.

El mié., 5 jun. 2019 a las 1:07, peter dalgaard (<[hidden email]>)
escribió:

> Two ideas:
>
> nn <- names(wageszm14)
> lvadd <- nn[grep("^lvacb", nn)]
>
> or
>
> lvadd <- paste0("lvacb", 23:81)
> lvadd <- lvadd[lvadd %in% names(wageszm14)]
>
> > On 5 Jun 2019, at 06:46 , Rolando I. Valdez via R-help <
> [hidden email]> wrote:
> >
> > Hello,
> >
> > I have almost 40 variables that I am trying to include in a formula.
> >
> > I tried to include them using as.formula(), however the variables do not
> > follow a patter in the name. e.g. These variables are named like: lvacb23
> > lvacb30 lvacb300  lvacb40 .....  lvacb81.
> >
> >> lvadd <- paste0("lvacb", 23:81)
> >> (fmla <- as.formula(paste("lwage ~ ", paste(lvadd, collapse = "+"))))
> >> fit <- lm(fmla, data = wageszm14)
> > Error in eval(predvars, data, env) : object 'lvacb24' not found
> >
> > The variable lvacb24 doesn't exist, because from lvacb23 it jumps to
> > lvacb30.
> >
> > Thanks in advance for any help.
> > --
> > Rolando Valdez
> > Facultad de Economía y Relaciones Internacionales
> > Universidad Autónoma de Baja California
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: [hidden email]  Priv: [hidden email]
>
>
>
>
>
>
>
>
>
>

--
Rolando Valdez
Facultad de Economía y Relaciones Internacionales
Universidad Autónoma de Baja California

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.