[Rd] Holding a large number of SEXPs in C++

2014-10-17 Thread Simon Knapp
Background: I have an algorithm which produces a large number of small polygons (of the spatial kind) which I would like to use within R using objects from sp. I can't predict the exact number of polygons a-priori, the polygons will be grouped into regions, and each region will be filled sequential

[Rd] model.matrix metadata

2014-10-17 Thread Patrick O'Reilly
Hi, As far as I am aware, the model.matrix function does not return perfect metadata on what each column of the model matrix "means". The columns are named (e.g. age:genderM), but encoding the metadata as strings can result in ambiguity. For example, the dummy variables created when the factors v

Re: [Rd] Holding a large number of SEXPs in C++

2014-10-17 Thread Simon Urbanek
On Oct 17, 2014, at 7:31 AM, Simon Knapp wrote: > Background: > I have an algorithm which produces a large number of small polygons (of the > spatial kind) which I would like to use within R using objects from sp. I > can't predict the exact number of polygons a-priori, the polygons will be > gr

Re: [Rd] model.matrix metadata

2014-10-17 Thread Charles Berry
Patrick O'Reilly gmail.com> writes: > > Hi, > > As far as I am aware, the model.matrix function does not return > perfect metadata on what each column of the model matrix "means". > > The columns are named (e.g. age:genderM), but encoding the metadata as > strings can result in ambiguity. For

[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) ) -

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) >

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 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 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
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