Thanks Martin. 

Here is my ''updated'' code. 

setClass("myClass", representation(ID.r = "numeric", ID.c = "character", DAT = "matrix")) 

to.myClass <- function(ID.r, ID.c, DAT) { 
    out <- new("myClass", ID.r = ID.r, ID.c = ID.c, DAT = DAT) 
    return(out) 
      } 
      
setMethod("[", "myClass", function(x, i, j, drop = TRUE) { 
    x@ID.r <- x@ID.r[i] 
    x@ID.c <- x@ID.c[j] 
    out.0 <- x@DAT[i,j] 
    out.1 <- to.myClass(x@ID.r, x@ID.c, as.matrix(out.0)) 
    return(out.1) 
  }) 
  
setMethod("[", c("myClass", "ANY", "character"), 
    function(x, i, j, ..., drop = TRUE) { 
    if(missing(i)) {x@ID.r <- x@ID.r} else {x@ID.r <- x@ID.r[i]} 
    j <- which(j == x@ID.c) 
    x@ID.c <- x@ID.c[j] 
    out.0 <- x@DAT[i, j] 
    out.1 <- to.myClass(x@ID.r, x@ID.c, as.matrix(out.0)) 
    return(out.1) 
      }) 
      
a <- to.myClass(seq(1,25), c("A","A","B","B"), matrix(rnorm(100), nrow = 25)) 
a 

a[1:20, ] #works 
a[, 1:3] #works 
a[1:10, 1:3] #works 
a[, "A"] #works 
a[5:20, "B"] #works 

It works, but Is it normal to write two codes for setMethod??? 

Nice weekend, OV 


________________________________
From: Martin Morgan <mtmorgan@fhcrc.org>

Cc: "r-help@r-project.org" <r-help@r-project.org>
Sent: Saturday, October 22, 2011 3:50 PM
Subject: Re: [R] setMethod "[" - extract by names within Slot

On 10/22/2011 02:11 AM, Omphalodes Verna wrote:
> Hi R-helper!
>
> I have problem with setMethods for "[". Here is example :
>
> setClass("myClass", representation(ID.r = "numeric", ID.c = "character", DAT = "matrix"))
>
> to.myClass<- function(ID.r, ID.c, DAT) {
>      out<- new("myClass", ID.r = ID.r, ID.c = ID.c, DAT = DAT)
>      return(out)
>        }
>
> setMethod("[", "myClass", function(x, i, j, drop) {
>      x@ID.r<- x@ID.r[i]
>      x@ID.c<- x@ID.c[j]
>      out.0<- x@DAT[i,j]
>      out.1<- to.myClass(x@ID.r, x@ID.c, as.matrix(out.0))
>      return(out.1)
>    })
>
> a<- to.myClass(seq(1,25), c("A","A","B","B"), matrix(rnorm(100), nrow = 25))
> a
>
>
> a[1:20, ] #works
> a[, 1:3] #works
> a[1:10, 1:3] #works
>
> a[, "A"] #not works

thinking about your code, this is the same as

>   ID.c = c("A","A","B","B")
>   j = "A"
>   ID.c[j]
[1] NA


>
> What is solution to write "[" methods for extraction by names of Slot "ID.c"

Maybe (untested)

   setMethod("[", c("myClass", "ANY", "character"),
       function(x, i, j, ..., drop=TRUE) {
           j = match(j, x@ID.c)
           x[i, j, ..., drop=TRUE]
      })

>
> Thanks all. OV
>
>     [[alternative HTML version deleted]]
>
>
>
>
> ______________________________________________
> R-help@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.


-- 
Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109

Location: M1-B861
Telephone: 206 667-2793
	[[alternative HTML version deleted]]

