[R] Using data and subset arguments in user-defined functions

From: Manuel Morales <Manuel.A.Morales_at_williams.edu>
Date: Fri 27 Oct 2006 - 21:18:05 GMT

Dear list,

A while ago, I posted a question asking how to use data or subset arguments in a user-defined function. Duncan Murdoch suggested the following solution in the context of a data argument:

data <- data.frame(a=c(1:10),b=c(1:10))

eg.fn <- function(expr, data) {

                  x <- eval(substitute(expr), envir=data)



I've tried various approaches to add a subset argument to the example above, but no luck. I'm looking for something like the following (but that works!)

eg.fn2 <- function(expr, data, subset) {

                   data <- subset(data,subset)
                   x <- eval(substitute(expr), envir=data)


eg.fn2(a, data, subset=a>3)

This returns the error:
"Error in eg.fn2(a, data, subset = a > 3) :

        object "a" not found"

Any suggestions?


Manuel A. Morales

______________________________________________ R-help@stat.math.ethz.ch 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.

Received on Sat Oct 28 07:26:29 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 27 Oct 2006 - 22:30:14 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.