[R] Help with the generic function "["

From: Gregory Wall <gdwall_at_ucdavis.edu>
Date: Mon, 14 Jan 2008 23:17:24 -0800 (PST)

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:


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"))


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:


                          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.

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue 15 Jan 2008 - 07:24:04 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 15 Jan 2008 - 07:30:07 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive