[R] can a matrix of lists work in R?

Tony Plate tplate at blackmesacapital.com
Tue Mar 12 19:05:09 CET 2002


I'm trying to convert over some S-plus code to R, and am having problems 
where it uses a matrix of lists.

Here's some code that works as expected in S-plus:

 > x <- vector("list",6)
 > dim(x) <- c(2,3)
 > x[1,2] <- list(letters[10:11])
 > x
         [,1]         [,2]    [,3]
[1,] NULL, 0 character, 2 NULL, 0
[2,] NULL, 0 NULL, 0      NULL, 0
 > x[1,2]
[[1]]:
[1] "j" "k"

 > x[3]
[[1]]:
[1] "j" "k"

 > x[[3]]
[1] "j" "k"
 >

The same commands in R do not produce the desired results (the assignment 
of element 1,2 does not work, I can assign in a different way, but then 
different types of indexing produce different results):
 > x <- vector("list",6)
 > dim(x) <- c(2,3)
 > x[1,2] <- list(letters[10:11])     # Can't assign using array indexing
Error: incompatible types in subset assignment
 > x[[3]] <- letters[10:11]           # I can assign to the desired element
 > x
      [,1]   [,2]          [,3]       # and it seems to produce the matrix 
I want
[1,] "NULL" "Character,2" "NULL"
[2,] "NULL" "NULL"        "NULL"
 > x[1,2]                             # but I get a different element by 
array indexing!
[[1]]
NULL

 > x[[3]]                             # the element is there for list or 
vector indexing!
[1] "j" "k"
 > x[3]
[[1]]
[1] "j" "k"

 >

My original way of creating the matrix of NULL's does not work in R either:
 > matrix(vector("list", 6), nrow=2)
Error in matrix(vector("list", 6), nrow = 2) :
         Unimplemented feature in copyVector
 >

Is there anything I can do to make a matrix of lists work in R?

thanks,

Tony Plate
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://stat.ethz.ch/pipermail/r-help/attachments/20020312/e6cb73e3/attachment.html


More information about the R-help mailing list