[R] Making an S3 object act like a data.frame

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Mar 9 10:54:18 CET 2006


On Tue, 7 Mar 2006, hadley wickham wrote:

>> I tend to have to use trial and error myself.  Here is another
>> possibility.
>
> That's got the subsetting solved, so here's the next challenge
>
>> lm(x ~ y, z)
> Error in as.data.frame.default(data) : cannot coerce class "myobj"
> into a data.frame
>> as.data.frame.myobj <- function(x) x[[1]]
>> lm(x ~ y, z)
> Error in eval(expr, envir, enclos) : numeric 'envir' arg not of length one
>
> I'm guessing this is pretty much impossible to get around, because
> there is no way to tell eval how to deal with myobj type objects, and
> lm only dispatches based on the type of the first argument.

Did you write an as.data.frame method?  From ?model.frame

     data: 'data.frame', list, 'environment' or object coercible to
           'data.frame' containing the variables in 'formula'.  Neither
           a matrix nor an array will be accepted.

so I believe that an as.data.frame method is all that is required.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list