[R] Question on passing the subset argument to an lm wrapper
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 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Question on passing the subset argument to an lm wrapper
On Sun, 24 Oct 2010, Erik Iverson wrote: 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 "...". The subset arg needs to be unevaluated until lm() - or whatever function - is called in your function. The canonical advice for this kind of question about passing unevaluated args is to study the first lines of the function lm noting what it does with objects cl <- match.call() and mf <- match.call( expand.dots = FALSE ). Something like this might be what you want: testlm2 <- function(formula, ...) { mc <- match.call() mc[[1]] <- as.name('lm') eval(mc) } testlm2(bmi ~ age, data= df1, subset = age > 50) HTH, Chuck ## 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 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Charles C. Berry(858) 534-2098 Dept of Family/Preventive Medicine E mailto:cbe...@tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Question on passing the subset argument to an lm wrapper
As workaround you can try this: testlm <- function(formula, ...) { args <- list(formula, data = df1, ...) do.call(lm, args) } testlm(bmi ~ age, subset = df1$age > 50) On Mon, Oct 25, 2010 at 2:16 AM, Erik Iverson wrote: > 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 > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.