Hi,

I'm having a hard time redefining the subset operation for arrays. The aim
is to be able to call a[dim2=1:5] instead of having to write a[,1:5,,,]
(assuming dimnames(a)[2]=-"dim2").
I already wrote a function that does this (see bottom).
But I just can't figure out how to redefine "[.array".
I tried 
"[.array"<-function(x,...) {cat("got here\n");42}
But this isn't called when I do
a=array(1:24,c(2,3,4))
a[1,2,3]
I tried
setMethod("[",  signature(x="array"),function(x,...) { cat("got here\n";42}
)
But that gives me an error:
--
Error in setMethod("[", signature(x = "array"), function(x, ...) { : 
  the method for function '[' and signature x="array" is sealed and cannot
be re-defined
--

Here is the function I'm trying to use:

"[.array"=function( input.array,...){                                           
                                                                                
                                           
  args=list(...)                                                                
                                                                                
                                            
  if( length(names(args))==0 ) {                                                
                                                                                
                                            
    d = args                                                                    
                                                                                
                                            
  } else {                                                                      
                                                                                
                                            
    d=dimnames( input.array )                                                   
                                                                                
                                            
    for(n in names(args)) {                                                     
                                                                                
                                            
      i=args[[n]]                                                               
                                                                                
                                            
      if( is.numeric(i)) {                                                      
                                                                                
                                            
        if( length(d[[n]]) == 0) d[[n]]=i                                       
                                                                                
                                            
        else d[[n]]=d[[n]][i]                                                   
                                                                                
                                            
      } else d[[n]]=args[[n]]                                                   
                                                                                
                                            
    }                                                                           
                                                                                
                                            
  }                                                                             
                                                                                
                                            
  dd=dim( input.array )                                                         
                                                                                
                                            
  for(i in seq(along=dd))                                                       
                                                                                
                                            
    if( length(d[[i]])==0) d[[i]] = seq( dd[i])                                 
                                                                                
                                            
  i=sapply(d,length)>1                                                          
                                                                                
                                            
  new.dimnames=dimnames(input.array)[i]                                         
                                                                                
                                            
  d=c(list( input.array ),d)                                                    
                                                                                
                                            
  ret = do.call(".subset",d)                                                    
                                                                                
                                            
  if( length(dim(ret))>0 )                                                      
                                                                                
                                            
    dimnames(ret) = new.dimnames                                                
                                                                                
                                            
  ret                                                                           
                                                                                
                                            
} 
---


--
View this message in context: 
http://r.789695.n4.nabble.com/redefining-subset-for-array-tp4589280p4589280.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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.

Reply via email to