8

I have an R object called gene_table, and it has class foo. Now I subset this gene_table by

gene_data = gene_table[1:100,1:5]

However, when I call class(gene_data), it is no longer of class foo, but instead, it has class matrix. This is a headache for me because my method summary.foo won't recognize this object gene_data of class matrix. I am hoping to retain the original class attribute when subsetting, so could anyone tell me how to do it? Thanks!

Update: dput(head(gene_table)) gives me

c(5.21708054951994, 5.01224214039806, 4.92160314073853, 4.83031021496, 4.78552614584879, 4.77821370665578)

and str(gene_table) gives me

 foo [1:22743, 1:2] 5.22 5.01 4.92 4.83 4.79 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:22743] "ENSG00000127954" "ENSG00000151503" "ENSG00000096060" "ENSG00000091879" ...
  ..$ : chr [1:2] "Var1" "Var2"
Maiasaura
  • 32,226
  • 27
  • 104
  • 108
alittleboy
  • 10,616
  • 23
  • 67
  • 107
  • @JoshuaUlrich: would you be more specific? Is [.foo when subsetting? Thanks! – alittleboy Sep 26 '12 at 20:47
  • Can you supply the output of `dput(head(gene_table))`? I'm guessing that it is not really of class `foo` and that there will need to look at the extraction methods for the real class. – IRTFM Sep 26 '12 at 20:47
  • 1
    He means you may need to write a method for `[` corresponding to `foo` (like how you did with `summary`). – joran Sep 26 '12 at 20:49
  • @DWin: if subsetting drops the class, then `head` will have the same effect because it subsets the object. The output of `str` shows that `gene_table` has class of `"foo"`. – Joshua Ulrich Sep 26 '12 at 20:57
  • 3
    From `?"["`: Subsetting (except by an empty index) will drop all attributes except names, dim and dimnames. – James Sep 26 '12 at 21:00

1 Answers1

6

You could use something like this as your definition for [.foo:

`[.foo` <- function(x, ..., drop=TRUE) {
   structure(NextMethod(), class="foo")
}

You may need to add other things, depending on the complexity of your "foo" class.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • Much more elegant. Should class also be set to `class=class(x)` in case it is a vector of several classes? – Josh O'Brien Sep 26 '12 at 21:22
  • @JoshO'Brien: yeah, I'll just say that's in the set of "other things". ;-) – Joshua Ulrich Sep 26 '12 at 21:29
  • OK, missed that. I have to remember -- just 'cause the code box is highlighted doesn't mean it's the *only* part I need to pay attention to! – Josh O'Brien Sep 26 '12 at 21:31
  • @JoshO'Brien @Joshua Ulrich: Thanks a lot for the codes! After running this code, I see that `gene_data` has class of `foo`. One thing I am still not sure: if I am writing a package, how can I include this ``[.foo`` function? Do I open a new R file and document this function like other functions? – alittleboy Sep 26 '12 at 21:35
  • @alittleboy: you need to read [Writing R Extensions](http://cran.r-project.org/doc/manuals/R-exts.html). Specifically pay attention to the section on [Registering S3 methods](http://cran.r-project.org/doc/manuals/R-exts.html#Registering-S3-methods). – Joshua Ulrich Sep 26 '12 at 21:38
  • @JoshuaUlrich: Thank you so much! I will read the document more carefully...I just realized that I missed something important in this file. – alittleboy Sep 26 '12 at 23:10