Vitalie Spinu
2008-Dec-01 17:06 UTC
[R] Non-interactive passing of quoted variables (ggplot, plyr, subset, transform, etc)
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()} this does not work since X and Y are not in mydata. Many, many thanks, Vitalie.
hadley wickham
2008-Dec-01 17:29 UTC
[R] Non-interactive passing of quoted variables (ggplot, plyr, subset, transform, etc)
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/