passing variable names to dplyr

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

passing variable names to dplyr

Bos, Roger-2
All,

I would like to figure out how to pass variable names to the dplyr function mutate.  For example, this works because hp is one of the variable names on mtcars:

mutate(mtcars, scale(hp))

Let's says I want to pass in the target variable instead of hard-coding the name, as follows:

target <- "hp"
mutate(mtcars, scale(target))

That dones't work.  I read somewhere about using lapply, but that suggestion didn't work for me either:

target <- lapply("hp", as.symbol)
mutate(mtcars, scale(target))

Does anyone know how to do this?

Thanks,

Roger

***************************************************************
This message is for the named person's use only. It may
contain confidential, proprietary or legally privileged
information. No right to confidential or privileged treatment
of this message is waived or lost by an error in transmission.
If you have received this message in error, please immediately
notify the the sender by e-mail, delete the message and all
copies from your system and destroy any hard copies.  You must
not, directly or indirectly, use, disclose, distribute,
print or copy any part of this message if you are not
the intended recipient.
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: passing variable names to dplyr

David Winsemius

On Jan 27, 2014, at 7:45 AM, Bos, Roger wrote:

> All,
>
> I would like to figure out how to pass variable names to the dplyr function mutate.  For example, this works because hp is one of the variable names on mtcars:
>
> mutate(mtcars, scale(hp))
>
> Let's says I want to pass in the target variable instead of hard-coding the name, as follows:
>
> target <- "hp"
> mutate(mtcars, scale(target))
>
> That dones't work.  I read somewhere about using lapply, but that suggestion didn't work for me either:
>
> target <- lapply("hp", as.symbol)
> mutate(mtcars, scale(target))
>
> Does anyone know how to do this?

You cross-posted this to the manipulatr newsgroup (where it was addressed). Crossposting is a practice which is not appreciated in R lists.

--

David Winsemius
Alameda, CA, USA

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: passing variable names to dplyr

djmuseR
David:

I privately suggested he post to manipulatr because Hadley is more
likely to see his question there first than in R-help. He originally
posted here, noted the cross-posting and referral at manipulatr and
responded back to this list when he got a successful reply from
Hadley. I don't see that he's done anything wrong; in fact, he's been
exceptionally polite. If you want to yell at anybody, yell at me.

Dennis

On Tue, Jan 28, 2014 at 3:33 PM, David Winsemius <[hidden email]> wrote:

>
> On Jan 27, 2014, at 7:45 AM, Bos, Roger wrote:
>
>> All,
>>
>> I would like to figure out how to pass variable names to the dplyr function mutate.  For example, this works because hp is one of the variable names on mtcars:
>>
>> mutate(mtcars, scale(hp))
>>
>> Let's says I want to pass in the target variable instead of hard-coding the name, as follows:
>>
>> target <- "hp"
>> mutate(mtcars, scale(target))
>>
>> That dones't work.  I read somewhere about using lapply, but that suggestion didn't work for me either:
>>
>> target <- lapply("hp", as.symbol)
>> mutate(mtcars, scale(target))
>>
>> Does anyone know how to do this?
>
> You cross-posted this to the manipulatr newsgroup (where it was addressed). Crossposting is a practice which is not appreciated in R lists.
>
> --
>
> David Winsemius
> Alameda, CA, USA
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: passing variable names to dplyr

David Winsemius

On Jan 28, 2014, at 3:43 PM, Dennis Murphy wrote:

> David:
>
> I privately suggested he post to manipulatr because Hadley is more
> likely to see his question there first than in R-help. He originally
> posted here, noted the cross-posting and referral at manipulatr and
> responded back to this list when he got a successful reply from
> Hadley. I don't see that he's done anything wrong; in fact, he's been
> exceptionally polite. If you want to yell at anybody, yell at me.

And after I replied I saw that he also posted a "solved" message in rhelp, a courtesy I do heartily endorse, so thank you, Roger. (If it were posted as a reply to his original question, I would not have sent anything. I didn't think I was yelling at anyone, just advising about list mores.)

--
David

>
> Dennis
>
> On Tue, Jan 28, 2014 at 3:33 PM, David Winsemius <[hidden email]> wrote:
>>
>> On Jan 27, 2014, at 7:45 AM, Bos, Roger wrote:
>>
>>> All,
>>>
>>> I would like to figure out how to pass variable names to the dplyr function mutate.  For example, this works because hp is one of the variable names on mtcars:
>>>
>>> mutate(mtcars, scale(hp))
>>>
>>> Let's says I want to pass in the target variable instead of hard-coding the name, as follows:
>>>
>>> target <- "hp"
>>> mutate(mtcars, scale(target))
>>>
>>> That dones't work.  I read somewhere about using lapply, but that suggestion didn't work for me either:
>>>
>>> target <- lapply("hp", as.symbol)
>>> mutate(mtcars, scale(target))
>>>
>>> Does anyone know how to do this?
>>
>> You cross-posted this to the manipulatr newsgroup (where it was addressed). Crossposting is a practice which is not appreciated in R lists.
>>
>> --
>>
>> David Winsemius
>> Alameda, CA, USA
>>
>> ______________________________________________
>> [hidden email] 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.

David Winsemius
Alameda, CA, USA

______________________________________________
[hidden email] 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.