[R] list of variables of an expression

Marc Schwartz marc_schwartz at me.com
Wed Jun 16 00:28:35 CEST 2010


On Jun 15, 2010, at 5:07 PM, David Winsemius wrote:

> 
> On Jun 15, 2010, at 5:45 PM, Alberto Lusiani wrote:
> 
>> I would like to get the list of variables of an expression: is that
>> possible?  I searched the help and the web without success.  In
>> practice, given an expression like
>> 
>> expr = parse(text="x^2+y^3")
>> 
>> I would like to have a function such as:
>> 
>> vars(expr)
>> 
>> returns
>> 
>> c("x", "y") or a list like the one returned by formals().
> 
> attr(terms(formula(paste("~", "x^2+y^3"))), "variables")
> # returns list(x, y)
> 


A little easier:

> all.vars(expression(x^2+y^3))
[1] "x" "y"


See ?all.vars for more information.

HTH,

Marc Schwartz



More information about the R-help mailing list