[R] Help with the generic function "["

Gregory Wall gdwall at ucdavis.edu
Tue Jan 15 08:17:24 CET 2008


Hello Everyone:

I'm hoping to get some suggestions on this problem i'm having with
subsetting on an R object i've created. I would really appreciate any advice. 

I've created this simple S3 class:

>str(ans)
Class 'myObj'  atomic [1:45000] 0.0428 0.0423 0.0554 0.0338 0.0345 ...
  ..- attr(*, "Size")= int 10
  ..- attr(*, "Region")= chr "EXON"


I create instances of this class by doing something like this:

ans <- structure(x,class="myObj", Size=length(i), Region=attr(x,"UTR"))

where x is a vector of reals. 

When I subset it via ans[c(2,3,4),c(2,4,8)] I get the result I want (my
subsetting works as if its a two dimensional object for reasons I'll omit
since I don't think its pertinent to the problem).

I've written my subsetting code in C, so my R function is basically just a
wrapper function to a .Call. 

"[.myObj" <- function(x, i=NA, j=NA, ...){

.. do some parameter checking...
.. then pass the data on to my C routine...

ans <- .Call("foo",as.numeric(x),as.integer(i),as.integer(j))
ans <- structure(ans,class="myObj", Size=length(i), Region=attr(x,"UTR"))

return(ans)
}

One of my main goals is to do this subsetting quickly (which is why i do
the subsetting in C) However, when I look at my results returned from
summaryRprof I see these 3 lines that are troubling:

$by.total
                          total.time total.pct self.time self.pct
"["                            41.54      50.3      0.22      0.3
"[.myObj"                      41.32      50.0      0.32      0.4
"as.numeric"                   38.58      46.7     38.58     46.7

these times are much much longer than they should be. I'm not sure why "["
is called twice.

I'm guessing that its being called once for the myObj class, and once for
the underlying atomic structure of a vector of reals. 

I've been looking at this issue for a while and trying to mimic other
subsetting packages but with no luck. I think the solution involves
understanding NextMethod("[") but then again I'm not so sure.

Again, I really appreciate any comments or advice!

Thanks a bunch,

Greg W.




More information about the R-help mailing list