Skip to content

subset drops S3 classes?

3 messages · Murat Tasan

#
Hi all --- I've stumbled upon some pretty annoying behavior, and I'm
curious how others may have gotten around it.
When using subset(...) on a data frame that contains a custom S3
field, the class is dropped in the result:
'data.frame':   10 obs. of  2 variables:
  $ x:Class 'MyClass'  int [1:10] 1 2 3 4 5 6 7 8 9 10
  $ y: int  10 9 8 7 6 5 4 3 2 1
'data.frame':   5 obs. of  2 variables:
  $ x: int  2 4 6 8 10
  $ y: int  9 7 5 3 1

And so, any generic functions hooked to MyClass suddenly don't work on
the subset results, but do work on the original data frame.
I think I could write a custom as.data.frame.MyClass for all such
classes, but this is annoying, indeed (and I don't know for sure if
that's a robust solution)
Wrapping in I(...) doesn't work, either:
'data.frame':   10 obs. of  2 variables:
  $ x:Classes 'AsIs', 'MyClass'  int [1:10] 1 2 3 4 5 6 7 8 9 10
  $ y: int  10 9 8 7 6 5 4 3 2 1
'data.frame':   5 obs. of  2 variables:
  $ x:Class 'AsIs'  int [1:5] 2 4 6 8 10
  $ y: int  9 7 5 3 1

(note that while 'AsIs' is kept, 'MyClass' has been removed in $x)

Cheers!

-Murat
#
And as a follow-up, I implemented a barebones as.data.frame.MyClass(...).
It works when dealing with non-subsetted data frames, but fails upon a
subset(...) call:
This works for a single column, e.g.:
'data.frame':   10 obs. of  1 variable:
  $ MyClass.1.10.:Class 'MyClass'  int [1:10] 1 2 3 4 5 6 7 8 9 10

But not during a subset:
'data.frame':   5 obs. of  1 variable:
 $ x: int  2 4 6 8 10

-Murat
On Wed, Nov 12, 2014 at 10:02 PM, Murat Tasan <mmuurr at gmail.com> wrote:
#
... aaaand nevermind, figured it out (from the final example on the
Extract.data.frame page):

`[.MyClass` <- function(x, i, ...) {
    NextMethod("[")
    mostattributes(RV) <- attribute(x)
    RV
}

cheers,

-m
On Wed, Nov 12, 2014 at 11:02 PM, Murat Tasan <mmuurr at gmail.com> wrote: