[R] Question on passing the subset argument to an lm wrapper

Erik Iverson eriki at ccbr.umn.edu
Mon Oct 25 06:16:09 CEST 2010


Hello,

How would you go about handling the following situation?
This is on R 2.12.0 on Ubuntu 32-bit.

I have a wrapper function to lm.  I want to pass in a
subset argument.  First, I just thought I'd use "...".

## make example reproducible
set.seed(123)
df1 <- data.frame(age = rnorm(100, 50, 10),
                   bmi = rnorm(100, 30, sd = 2))

## create a wrapper using "..."
testlm <- function(formula, ...) {
   lm(formula, data = df1, ...)
}

 > testlm(bmi ~ age, subset = age > 50)

Error in eval(expr, envir, enclos) :
   ..1 used in an incorrect context, no ... to look in

I found some other examples of this error message,
but couldn't piece together how it fits in with this
example.

Next, I tried specifying a subset argument.

testlm2 <- function(formula, subset) {
   lm(formula, data = df1, subset = subset)
}

 > testlm2(bmi ~ age, subset = age > 50)

Error in xj[i] : invalid subscript type 'closure'

I also don't understand this one.

Any pointers on if I'm just missing the easy
solution to do what I want?  Any explanations
as to the above behavior (I know it has to do
with model.frame, but not sure how) would also
be greatly appreciated!

Thanks!
--Erik



More information about the R-help mailing list