[Rd] list/matrix chimera

Tony Plate tplate at blackmesacapital.com
Tue Apr 10 17:02:44 CEST 2007


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
>



More information about the R-devel mailing list