repeating the same variable in formula

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

repeating the same variable in formula

isubirana
I am using terms.formula function to substract the variables from a formula environment.

Concretely, with the attr(*, "term.labels") I get the right side terms specified in the formula.

However, when introducing the same variable two or more times, this appears only once.

For example, when typing



>f <- y ~ cholesterol + age + age

>attr(terms(f),"term.labels")



I get



[1] "cholesterol" "age"



and I would like to get



[1] "cholesterol" "age" "age"





Is it possible with terms.formula function to obtain the variables and the times that they are specified in the formula? Or may I use another function?



Thanks in advance.



Isaac.


        [[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: repeating the same variable in formula

Rui Barradas
Hello,

The formulas

y ~ cholesterol + age + age

and

y ~ cholesterol + age

are the same formula.
If you want 'age' twice, maybe

g <- y ~ cholesterol + I(age + age)
attr(terms(g), "term.labels")
#[1] "cholesterol"  "I(age + age)"


Hope this helps,

Rui Barradas

Às 06:49 de 10/12/2018, Subirana Cachinero, Isaac escreveu:
> y ~ cholesterol + age + age

______________________________________________
[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: repeating the same variable in formula

isubirana
Thank you for your response.
In fact, I use the formula environament to select variables, as part of the code of another function.
I would like to allow the user to select the same variable more than once.
The use of I() may partly solve the problem. However, I would like attr(terms(g),"term.labels") would return a vector with as many components as variables including repetitions. And using I() function, there would be some work remaining to split "I(age + age)" into "age", "age", taking into account that in other examples variable may include white spaces or other characters within ` `.

Isaac.

-----Mensaje original-----
De: Rui Barradas [mailto:[hidden email]]
Enviado el: lunes, 10 de diciembre de 2018 11:27
Para: Subirana Cachinero, Isaac; [hidden email]
Asunto: Re: [R] repeating the same variable in formula

Hello,

The formulas

y ~ cholesterol + age + age

and

y ~ cholesterol + age

are the same formula.
If you want 'age' twice, maybe

g <- y ~ cholesterol + I(age + age)
attr(terms(g), "term.labels")
#[1] "cholesterol"  "I(age + age)"


Hope this helps,

Rui Barradas

Às 06:49 de 10/12/2018, Subirana Cachinero, Isaac escreveu:
> y ~ cholesterol + age + age

______________________________________________
[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: repeating the same variable in formula

Peter Dalgaard-2
You might be looking for this:

> all.vars(~chol+age+age, unique=FALSE)
[1] "chol" "age"  "age"


-pd


> On 10 Dec 2018, at 11:35 , Subirana Cachinero, Isaac <[hidden email]> wrote:
>
> Thank you for your response.
> In fact, I use the formula environament to select variables, as part of the code of another function.
> I would like to allow the user to select the same variable more than once.
> The use of I() may partly solve the problem. However, I would like attr(terms(g),"term.labels") would return a vector with as many components as variables including repetitions. And using I() function, there would be some work remaining to split "I(age + age)" into "age", "age", taking into account that in other examples variable may include white spaces or other characters within ` `.
>
> Isaac.
>
> -----Mensaje original-----
> De: Rui Barradas [mailto:[hidden email]]
> Enviado el: lunes, 10 de diciembre de 2018 11:27
> Para: Subirana Cachinero, Isaac; [hidden email]
> Asunto: Re: [R] repeating the same variable in formula
>
> Hello,
>
> The formulas
>
> y ~ cholesterol + age + age
>
> and
>
> y ~ cholesterol + age
>
> are the same formula.
> If you want 'age' twice, maybe
>
> g <- y ~ cholesterol + I(age + age)
> attr(terms(g), "term.labels")
> #[1] "cholesterol"  "I(age + age)"
>
>
> Hope this helps,
>
> Rui Barradas
>
> Às 06:49 de 10/12/2018, Subirana Cachinero, Isaac escreveu:
>> y ~ cholesterol + age + age
>
> ______________________________________________
> [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: repeating the same variable in formula

isubirana
Thank you very much.
This is exactly what I needed.

Isaac.

-----Mensaje original-----
De: peter dalgaard [mailto:[hidden email]]
Enviado el: lunes, 10 de diciembre de 2018 15:10
Para: Subirana Cachinero, Isaac
CC: Rui Barradas; [hidden email]
Asunto: Re: [R] repeating the same variable in formula

You might be looking for this:

> all.vars(~chol+age+age, unique=FALSE)
[1] "chol" "age"  "age"


-pd


> On 10 Dec 2018, at 11:35 , Subirana Cachinero, Isaac <[hidden email]> wrote:
>
> Thank you for your response.
> In fact, I use the formula environament to select variables, as part of the code of another function.
> I would like to allow the user to select the same variable more than once.
> The use of I() may partly solve the problem. However, I would like attr(terms(g),"term.labels") would return a vector with as many components as variables including repetitions. And using I() function, there would be some work remaining to split "I(age + age)" into "age", "age", taking into account that in other examples variable may include white spaces or other characters within ` `.
>
> Isaac.
>
> -----Mensaje original-----
> De: Rui Barradas [mailto:[hidden email]]
> Enviado el: lunes, 10 de diciembre de 2018 11:27
> Para: Subirana Cachinero, Isaac; [hidden email]
> Asunto: Re: [R] repeating the same variable in formula
>
> Hello,
>
> The formulas
>
> y ~ cholesterol + age + age
>
> and
>
> y ~ cholesterol + age
>
> are the same formula.
> If you want 'age' twice, maybe
>
> g <- y ~ cholesterol + I(age + age)
> attr(terms(g), "term.labels")
> #[1] "cholesterol"  "I(age + age)"
>
>
> Hope this helps,
>
> Rui Barradas
>
> Às 06:49 de 10/12/2018, Subirana Cachinero, Isaac escreveu:
>> y ~ cholesterol + age + age
>
> ______________________________________________
> [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.