Skip to content
Back to formatted view

Raw Message

Message-ID: <49F19985.6080705@stats.uwo.ca>
Date: 2009-04-24T10:50:45Z
From: Duncan Murdoch
Subject: Generalized 2D list/array/whatever?
In-Reply-To: <c78ddbe30904231903t59943544od30fc3d42c6c50fc@mail.gmail.com>

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:

x[[c("some label", "some other index")]] <- 3

This assumes that x is a list, and one of its entries is a list named 
"some label".  It will not create that entry, but it will create an 
entry named "some other index", so you need

if (is.null(x[["some label"]])) x[["some label"]] <- list()

first.  After executing this line and your first line above, you'll get

 > x
$`some label`
$`some label`$`some other index`
[1] 3

Duncan Murdoch