2

I really plyr's quoted class; I'd like to be able to able to combine two quoted objects and get a new one. For example, how do I define mult(a,b) such that

q1 <- as.quoted("x+y")
q2 <- as.quoted("y+z")
mult <- function(a, b) {?????}

## mult(q1, q2) returns as.quoted("(x+y)*(y+z)")

1 Answers1

0

You need to eval the quoted formulas then paste them together:

mult <- function(a, b) {
  as.quoted(paste(eval(a), '*', eval(b)))
}

> mult(q1, q2)
List of 1
 $ x + y * y + z: language x + y * y + z
 - attr(*, "env")=<environment: 0x2920980> 
 - attr(*, "class")= chr "quoted"
> 
Justin
  • 42,475
  • 9
  • 93
  • 111
  • 1
    It looks like you can! In R there are almost always tons of ways to do things. It also may depend on your final use case for the formula. I used `eval` since that is the "standard" opposite of `as.quoted`. The is particularly true for the `data.table` package. See [here](http://stackoverflow.com/questions/11872499/create-an-expression-from-a-function-for-data-table-to-eval) for an example. – Justin Sep 04 '12 at 14:32