Skip to content

Subsetting using dimnames on S4 array-based class

5 messages · Iago Mosqueira, Brian Ripley, Wolski

#
Hello,

I did send this message to r-help and got no reply, no I am resubmitting
here in case this was a bit too specific for the other list.

Many thanks,


Iago


                             From: 
Iago Mosqueira
<imosqueira@suk.azti.es>
                               To: 
r-help@stat.math.ethz.ch
                          Subject: 
Subsetting using dimnames on S4
array-based class
                             Date: 
Fri, 11 Feb 2005 08:29:03 +0000

Hello,

I am encountering some problems when overloading the "[" operator for a
new S4 class based on array. This is an example class definition:

setClass("foo",
        representation("array"),
        prototype(array(NA, dim=c(3,3)), 
        dimnames=list(age=1:3, year=10:12))
)

And this the corresponding setMethod with print estatements to see what
is being passed:

setMethod("[", signature(x="foo"),
    function(x, i="missing", j="missing", ..., drop="missing") {
        print(paste("i:", i))
        print(paste("j:", j))
     }
)


So I first create a new object and load it with some data:
And then apply subsetting without using the dimension names and see what
are the values of i and j inside the function:
[1] "i: 1" "i: 2"
[1] "j: 10"


Both i and j hold exactly what was expected here. But if I use the
dimension names, the subsetting indices does not seem to be passed as I
expected:
[1] "i: missing"
[1] "j: missing"
[1] "i: missing"
[1] "j: missing"

Subsetting with dimnames appears to work without trouble on an array,
which "foo" extends:

s<-array(1:9,dim=c(3,3),dimnames=list(age=1:3,year=1:3))
2 3 
4 7
2 3 
4 7

Although dimnames seem to be in fact simply ignored:
[1] 7


System:
Linux Debian 3.0
R 2.0.0

Do I need to define my class differently for subsetting using dimnames
to work? Even if they are not really being checked, I would like to be
able to use subsetting in this way as it makes code more readable when
using arrays with many dimensions.

Many thanks,


Iago Mosqueira
#
On Thu, 17 Feb 2005, Iago Mosqueira wrote:

            
Do read the posting guide before posting, as we ask.  It has clear 
guidelines on this.

Your problem seems to be that you want to use named arguments in 
subscripting (not really anything to do with your subject: using dimnames 
is like

s["row 1", "col 2"]

and you are talking about *names of* dimnames).

It `works' for arrays because the definition there (in ?Extract) is not 
the same as the generic you are using: notice the ... in the definitions, 
and for arrays it is really "["(x, ..., drop=TRUE) and the names of ... 
are ignored.

In brief: this is not how [ in R works.

Be careful:

m <- matrix(1:6, 2, 3)
M <- data.frame(a=1:2, b=3:4, c=5:6)
m[j=2, i=1] # 2
M[j=2, i=1] # 3

so argument names are ignored for the primitives, but not for S3 methods
(and I believe not for S4 methods).

  
    
#
On Thu, 2005-02-17 at 12:32, Prof Brian Ripley wrote:

            
Sorry for the confusion.
Thanks. I did realise for arrays the names are ignored, but in the new
class they are not even accepted.
I am afraid I fail to see then why my example code fails to accept names
when subsetting. Shouldn't a class that extends "array" inherit this
behaviour too?

Many thanks,


Iago
#
On Thu, 17 Feb 2005, Iago Mosqueira wrote:

            
No, it inherits from the S4 pseudo-method for "array", not the primitive.
Remember that S4 methods are bolted on to a different system, and bridging 
the gaps where possible has been very hard work (by John Chambers).
#
Hi,

The topic of extending S4 classes from S3 classes was discussed several 
times.
e.g.

S3 classes .... "don't have a consistent set of "slots" (they may or may 
not have a "dimnames), they aren't quite real classes in an S4 sense. It 
would be nice to fix this mess, but not obviously possible while being 
back compatible."

http://tolstoy.newcastle.edu.au/R/devel/05/01/1905.html


Eryk

Ps. A google search: *r-devel S4 array list* etc. Will provide you with 
more references.
Iago Mosqueira wrote: