Re: [Rd] Most efficient way to check the length of a variable mentioned in a formula.

2014-10-21 Thread Joris Meys
Hi Duncan, thanks for your reaction. I'm not following completely though what you mean with "no guarantee that the length() function will do what I want if I evaluate it in an environment set by the user". I wasn't intending to give the user the opportunity to set those environments, but is there

Re: [Rd] Most efficient way to check the length of a variable mentioned in a formula.

2014-10-21 Thread Duncan Murdoch
On 17/10/2014, 2:23 PM, Gabriel Becker wrote: > Joris, > > For me > > length(environment(form)[["x"]]) > > Was about twice as fast as > > length(get("x",environment(form > > In the year-old version of R (3.0.2) that I have on the virtual machine i'm > currently using. Those are different:

Re: [Rd] Most efficient way to check the length of a variable mentioned in a formula.

2014-10-18 Thread Joris Meys
Thanks again William, I owe you one! Cheers Joris On Fri, Oct 17, 2014 at 11:36 PM, William Dunlap wrote: > In my example function I did not evaluate the formula either, just a part > of it. > > If you leave off the envir and enclos arguments to eval in your > function you can get surprising (wr

Re: [Rd] Most efficient way to check the length of a variable mentioned in a formula.

2014-10-17 Thread William Dunlap
In my example function I did not evaluate the formula either, just a part of it. If you leave off the envir and enclos arguments to eval in your function you can get surprising (wrong) results. E.g., > afun(y ~ varnames) [[1]] [1] 10 9 8 7 6 5 4 3 2 1 [[2]] [1] "y""va

Re: [Rd] Most efficient way to check the length of a variable mentioned in a formula.

2014-10-17 Thread Joris Meys
Thank you both, great ideas. William, I see the point of using eval, but the problem is that I can't evaluate the formula itself yet. I need to know the length of these variables to create a function that is used to evaluate. So if I try to evaluate the formula in some way before I created the fun

Re: [Rd] Most efficient way to check the length of a variable mentioned in a formula.

2014-10-17 Thread William Dunlap
I got the default value for getRHSLength's data argument wrong - it should be NULL, not parent.env(). getRHSLength <- function (formula, data = NULL) { rhsExpr <- formula[[length(formula)]] rhsValue <- eval(rhsExpr, envir = data, enclos = environment(formula)) length(rhsV

Re: [Rd] Most efficient way to check the length of a variable mentioned in a formula.

2014-10-17 Thread William Dunlap
I would use eval(), but I think that most formula-using functions do it more like the following. getRHSLength <- function (formula, data = parent.frame()) { rhsExpr <- formula[[length(formula)]] rhsValue <- eval(rhsExpr, envir = data, enclos = environment(formula)) length(rhsValue) }

Re: [Rd] Most efficient way to check the length of a variable mentioned in a formula.

2014-10-17 Thread Gabriel Becker
Joris, For me length(environment(form)[["x"]]) Was about twice as fast as length(get("x",environment(form In the year-old version of R (3.0.2) that I have on the virtual machine i'm currently using. As for you, the eval method was much slower (though my factor was much larger than 20) >

[Rd] Most efficient way to check the length of a variable mentioned in a formula.

2014-10-17 Thread Joris Meys
Dear R gurus, I need to know the length of a variable (let's call that X) that is mentioned in a formula. So obviously I look for the environment from which the formula is called and then I have two options: - using eval(parse(text='length(X)'), envir=environment(formula) ) -