[R] Generalized 2D list/array/whatever?

Duncan Murdoch murdoch at stats.uwo.ca
Fri Apr 24 15:04:06 CEST 2009


On 24/04/2009 8:48 AM, hadley wickham wrote:
> On Fri, Apr 24, 2009 at 5:50 AM, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
>> Toby wrote:
>>> I'm trying to figure out how I can get a generalized 2D
>>> list/array/matrix/whatever
>>> working.  Seems I can't figure out how to make the variables the right
>>> type.  I
>>> always seem to get some sort of error... out of bounds, wrong type, wrong
>>> dim, etc.
>>> Very confused... :)
>>>
>>> x[["some label", "some other index"]] <- 3
>>> x[["some other label", "something else"]] <- 4
>>>
>>> I don't know the indexes/label ahead of time... they get generated...  Any
>>> thoughts?
>>>
>> What you have there is not legal syntax, but this would be:
> 
> It isn't?

You're right, it is.  Sorry.

However, I think the solution I gave (not to make the list into an 
array, but rather to use a list of lists) is probably better in a lot of 
situations where the indices show up unpredictably.

Duncan Murdoch

> 
> a <- as.list(1:4)
> dim(a) <- c(2, 2)
> rownames(a) <- c("a", "b")
> colnames(a) <- c("c", "d")
> 
> a[["a", "d"]]
> 
> Hadley
> 
>




More information about the R-help mailing list