From tplate at blackmesacapital.com  Tue Apr 10 17:02:44 2007
From: tplate at blackmesacapital.com (Tony Plate)
Date: Tue, 10 Apr 2007 09:02:44 -0600
Subject: [Rd] list/matrix chimera
In-Reply-To: <m2lkh0xp4i.fsf@fhcrc.org>
References: <m2lkh0xp4i.fsf@fhcrc.org>
Message-ID: <461BA714.5040009@blackmesacapital.com>

Aren't you just seeing the effect of drop=TRUE? (at least with the 
examples you give below -- they all pick out a submatrix with extent one 
on some dimension)

AFAICT, matrices with a list as the underlying data work properly, e.g.:

 > vv <- array(as.list(1:12), 3:4)
 > vv
      [,1] [,2] [,3] [,4]
[1,] 1    4    7    10
[2,] 2    5    8    11
[3,] 3    6    9    12
 > vv[1:2,]
      [,1] [,2] [,3] [,4]
[1,] 1    4    7    10
[2,] 2    5    8    11
 > vv[1,,drop=FALSE]
      [,1] [,2] [,3] [,4]
[1,] 1    4    7    10
 >

It can actually be useful sometimes to have a matrix (or array) of 
non-atomic objects -- wouldn't your proposed change remove (or at least 
damage) that functionality?

-- Tony Plate

Seth Falcon wrote:
> 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
>



[Rd] list/matrix chimera
Tony Plate