[R] List to Array: How to establish the dimension of the array

Rui Barradas ruipbarradas at sapo.pt
Wed Jan 25 18:53:53 CET 2012


Hello,

If you just want the dimensions of the array, and assuming the input list
will give
a rectangular array with no need to use NA's, the function below works.
(It doesn't construct the array)

aa <- list(list(list(1:4),
                 list(5:8),
                 list(9:12)),
            list(list(13:16),
                 list(17:20),
                 list(21:24)))
bb <- list(aa, aa, aa, aa, aa)

fun <- function(lst){
	d <- ix <- 0
	while(length(lst) > 1){
		d[ix <- ix+1] <- length(lst)
		lst <- lst[[1]]
	}
	d[ix+1] <- length(lst[[1]])
	d
}

fun(aa)
fun(bb)

fun(list(aa, bb)) # not rectangular, therefore
fun(list(bb, aa)) # different results

Rui Barradas


--
View this message in context: http://r.789695.n4.nabble.com/List-to-Array-How-to-establish-the-dimension-of-the-array-tp4325622p4327970.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list