Skip to content
Prev 49172 / 63424 Next

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

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(rhsValue)
   }
so that the function firstHalf is found in the following
   > X <- 1:10
   > getRHSLength((function(){firstHalf<-function(x)x[seq_len(floor(length(x)/2))];
~firstHalf(X)})())
   [1] 5


Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Fri, Oct 17, 2014 at 11:57 AM, William Dunlap <wdunlap at tibco.com> wrote: