[Rd] Subsetting using dimnames on S4 array-based class

From: Iago Mosqueira <imosqueira_at_suk.azti.es>
Date: Thu 17 Feb 2005 - 23:37:56 EST

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:

> x <- new("foo")
> x[,] <- 1:9

And then apply subsetting without using the dimension names and see what are the values of i and j inside the function:

> x[1:2,'10']

[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:

> x[age=1:3, year=1:3]

[1] "i: missing"
[1] "j: missing"
> x[, year='10']

[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))
> s[1,2:3]

2 3

4 7
> s[age=1,year=2:3]

2 3
4 7

Although dimnames seem to be in fact simply ignored:

> s[a=1,b=3]

[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



R-devel@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Thu Feb 17 21:52:46 2005

This archive was generated by hypermail 2.1.8 : Fri 18 Mar 2005 - 09:02:53 EST