4

Is it possible to get a vector or list of variables in an expression?

For instance:

e <- expression(2 * x^2 + y)

The desired output:

('x', 'y')

Is it possible? Or is it necessary to input variable names manually?

David Angyal
  • 231
  • 4
  • 13
  • You'd be slightly better off creating the simpler call object here: `e <- quote(2 * x^2 + y)` – hadley Dec 03 '13 at 16:19

1 Answers1

9

Use all.vars:

all.vars(e)
[1] "x" "y"
James
  • 65,548
  • 14
  • 155
  • 193
  • And in case you want to write variations, http://adv-r.had.co.nz/Expressions.html#walking-the-call-tree-with-recursive-functions describes how to do it with recursive R code. – hadley Dec 03 '13 at 16:19