Skip to content
Back to formatted view

Raw Message

Message-ID: <f8e6ff050812010929h18d9068fu8d5a0d188bc49e5c@mail.gmail.com>
Date: 2008-12-01T17:29:36Z
From: Hadley Wickham
Subject: Non-interactive passing of quoted variables (ggplot, plyr, subset, transform, etc)
In-Reply-To: <op.ulhtspnqw9ljuw@tir-070312.staff.few.eur.nl>

On Mon, Dec 1, 2008 at 11:06 AM, Vitalie Spinu <vitosmail at rambler.ru> wrote:
> Hello Everyone,
> May be a silly question.
>
> How to pass programmatically variables which are not known in advance and
> are quoted? Variables are quoted implicitly in  functions like "subset" and
> "transform" and explicitly in ggplot and plyr.
>
> For instance I would like to have something like this:
>
> myfunc<-functon(mydata,X) subset(mydata,X>4)
> myfunc<-function(mydata,X,Y) {ggplot(mydata,aes(x=X,y=Y))+geom_points()}

For subset, just do it by hand:

mydata[mydata[[X]] > 4, ]

For ggplot, see aes_string:

aes_string(x = X, y = Y)
which will do what you want

For plyr, you can use a character vector:

ddply(mtcars, c("cyl"), colwise(mean))
ddply(mtcars, c("cyl ^ 2", "vs - am"), colwise(mean))

In general you need to use substitute or bquote.

Hadley

-- 
http://had.co.nz/