[Rd] list/matrix chimera

Seth Falcon sfalcon at fhcrc.org
Tue Apr 10 16:47:41 CEST 2007


Hi all,

If dimensions are added to a list, it will become a matrix-like hybrid
that calls itself a matrix, but returns lists for subset operations.

This was brought to my attention by a user that encountered such an
object and was quite confused by its behavior.  Although I have not
found the code that created the object yet, I believe that code is
simply incorrect.  Nevertheless, I wonder if R_data_class in attrib.c
should be a bit more discriminating and only return matrix or array
for atomic vectors.  Perhaps something like this:

--- a/src/main/attrib.c
+++ b/src/main/attrib.c
@@ -536,7 +536,7 @@ SEXP R_data_class(SEXP obj, Rboolean singleString)
     if(n == 0) {
        SEXP dim = getAttrib(obj, R_DimSymbol);
        int nd = length(dim);
-       if(nd > 0) {
+       if(nd > 0 && isVectorAtomic(obj)) {
            if(nd == 2)
                klass = mkChar("matrix");
            else

Here is an example that illustrates:

    vv <- as.list(1:24)
    dim(vv) <- c(6, 4)
    vv
         [,1] [,2] [,3] [,4]
    [1,] 1    7    13   19  
    [2,] 2    8    14   20  
    [3,] 3    9    15   21  
    [4,] 4    10   16   22  
    [5,] 5    11   17   23  
    [6,] 6    12   18   24  
    
    class(vv)
    [1] "matrix"
    
    typeof(vv)
    [1] "list"
    
    vv[1, 1]
    [[1]]
    [1] 1
    
    vv[1, ]
    [[1]]
    [1] 1
    
    [[2]]
    [1] 7
    
    [[3]]
    [1] 13
    
    [[4]]
    [1] 19


+ seth

-- 
Seth Falcon | Computational Biology | Fred Hutchinson Cancer Research Center
http://bioconductor.org



More information about the R-devel mailing list