[R] Using data=x or subset=y in user-defined functions

Duncan Murdoch murdoch at stats.uwo.ca
Thu Jun 8 21:18:04 CEST 2006


On 6/8/2006 2:56 PM, Manuel Morales wrote:
> On Thu, 2006-06-08 at 11:54 -0400, Manuel Morales wrote:
>> On Wed, 2006-06-07 at 21:36 +0100, Prof Brian Ripley wrote:
>> > I suggest you investigate with().
> 
> Thanks for the suggestion! Unfortunately, it's not clear to me how to
> call with() from a function. The example below fails with the error
> message: 'Error in mean(x) : object "a" not found'
> 
> data.test <- data.frame(a=c(1:10),b=rnorm(10))
> 
> eg.function <- function(x, data)
>   with(data, return(mean(x)))
> 
> eg.function(a,data.test)

When you call a function in R, you normally need to have the variables 
defined in the local environment.  You are trying to pass a variable 
named a to your function (where a copy will be made in the local 
variable x), but there is no variable a to pass.

I think what you want to do is to pass an expression "a" to your 
function, and have the function evaluate that expression within your 
dataframe, i.e. you don't want the standard evaluation method to be 
used. To do that you'd do something like this:

 > eg.function <- function(expr, data) {
+   x <- eval(substitute(expr), envir=data)
+   return(mean(x))
+ }
 > eg.function(a, data.test)
[1] 5.5

The "substitute" says to give back the unevaluated expression used for 
the argument.

Duncan Murdoch

> 
> Manuel
> 
>> Thanks! Just to be sure, the following will do what I want?
>> 
>> eg.function <- function(x, data=NULL, subset=NULL, ...) {
>>     with(if(is.null(subset)) data else subset(data,subset), {
>>  
>>     ...
>> 
>>     })
>> }
>> 
>> > On Wed, 7 Jun 2006, Manuel Morales wrote:
>> > 
>> > > Dear list members,
>> > >
>> > > In some of my functions, I attach the data internally to allow subset
>> > > commands or to specify a data frame. This works well except for cases
>> > > where there is a "masking" conflict (which returns a warning). I see
>> > > some alternative listed in ?attach, but I'm not sure which of them do
>> > > what I'd like. Any suggestions?
>> > >
>> > > Below is how I've been setting up my functions:
>> > >
>> > > 
>> > >
>> > > # Set up environment
>> > > on.exit(detach(data))
>> > > attach(data)
>> > > if(!is.null(subset)) {
>> > >    data<-subset(data,subset)
>> > > detach(data)
>> > > attach(data)
>> > > }
>> > > subset = NULL
>> > >
>> > > # Function body here
>> > > output <- x
>> > > return(output)
>> > > }
>> > >
>> > > Thanks!
>> > >
>> > > Manuel
>> > >
>> > > ______________________________________________
>> > > R-help at 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
>> > >
>> >
>> 
>> ______________________________________________
>> R-help at 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
> 
> ______________________________________________
> R-help at 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



More information about the R-help mailing list