[R] Dimensioning lists.

Rolf Turner r@turner @end|ng |rom @uck|@nd@@c@nz
Mon Feb 15 04:35:11 CET 2021

I have a setting in which it would be convenient to treat a list
as an array, i.e. to address its entries via a pair of indices.

A toy example:

xxx <- vector("list",9)
for(i in 1:9) xxx[[i]] <- list(a=sample(letters,1),b=sample(1:100,1))

I would like to be able to treat "xxx" as a 3 x 3 matrix.

I tried

   dim(xxx) <- c(3,3)

When I do, e.g.


I get:

> [[1]]
> [[1]]$a
> [1] "n"
> [[1]]$b
> [1] 20

That is I get a list of length 1, whose (sole) entry is the desired
object.  I would *like* to get just the desired object, *not* wrapped in
a list, i.e.:

> $a
> [1] "n"
> $b
> [1] 20

(which is what I get by typing xxx[2,3][[1]]).

Is there any way to prevent the entries of xxx from being wrapped up in
lists of length 1?

Thanks for any enlightenment.


Rolf Turner

Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

More information about the R-help mailing list