Re: [R] List to Array: How to establish the dimension of the array
Please include the context of the discussion in your responses. See inline below. On 1/24/2012 11:33 PM, Ajay Askoolum wrote: Thanks you, I can get the length of aa with length(unlist(aa)). If aa has 4 dimensions, I imagine I'd need to do max(sapply(aa,sapply,sapply,length) Correct. How can I do this in a generic way? That is in a loop. I am clear about the exit condition for the loop. By generic, I assume that you mean without knowing the depth of the lists (that is, "dimensions") to begin with? d<-1 start loop if d = length(unlist(aa)) then exit loop Note that length(unlist(aa)) will only equal the product of the dimensions if the data is "regular." That is, there is an entry for every combination of indices (within the dimensions). else d<-d * How do I construct such that it does d<- d * length(aa) # first pass d<- d * max(sapply(aa, length)) # second pass d<- d * max(sapply(aa, sapply, length)) # third pass # ? ## fourth path etc (Apologies for the pseudo code describing the loop; I am not near a machine with R) I don't really understand this. One way I can thing of is to create a loop counter variable, say lc<-1 and to increment it within the loop and then use a switch statement to execute the appropriate expression. This sems like a kludge to me. Is there a neater way? If you are trying to get back a vector of the dimensions, then this would work: dimRecursiveList <- function(l) { if (class(l) == "list") { c(length(l), dimRecursiveList(l[[1]])) } else { NULL } } From previous context: aa <- list(list(list(37531.52, 62787.32, 5503.184, 33832.8), list(20469.60, 27057.27, 51160.25, 45165.24), list(957.932, 21902.94, 37531.52, 62787.32)), list(list(5503.184, 33832.8, 20469.6, 27057.27), list(51160.25, 45165.24, 957.932, 21902.94), list(37531.52, 62787.32, 5503.184, 33832.8))) Which then gives > dimRecursiveList(aa) [1] 2 3 4 In this case, the data is regular, so > Reduce(`*`, dimRecursiveList(aa)) == length(unlist(aa)) [1] TRUE If the data are not regular, and you want the dimension to be the largest, then it is more complicated (due to bookkeeping) dimRecursiveListIrregular <- function(l) { if (class(l) == "list") { dims <- sapply(l, dimRecursiveListIrregular) if (is.null(dim(dims))) { if (all(is.na(dims))) { length(l) } else { c(length(l), max(dims, na.rm=TRUE)) } } else { c(length(l), apply(dims, 1, max, na.rm=TRUE)) } } else { NA } } If the data are regular, then it is better to convert this to an array. This function will do it for arbitrary depth RecursiveListToArray <- function(l) { if(class(l) == "list") { laply(l, RecursiveListToArray) } else { l } } > aaa <- RecursiveListToArray(aa) > dim(aaa) [1] 2 3 4 -- Brian S. Diggs, PhD Senior Research Associate, Department of Surgery Oregon Health & Science University __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] List to Array: How to establish the dimension of the array
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. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] List to Array: How to establish the dimension of the array
Thanks you, I can get the length of aa with length(unlist(aa)). If aa has 4 dimensions, I imagine I'd need to do max(sapply(aa,sapply,sapply,length) How can I do this in a generic way? That is in a loop. I am clear about the exit condition for the loop. d<-1 start loop if d = length(unlist(aa)) then exit loop else d<-d * How do I construct such that it does > d<- d * length(aa) # first pass > d<- d * max(sapply(aa, length)) # second pass > d<- d * max(sapply(aa, sapply, length)) # third pass > # ? # # fourth path etc (Apologies for the pseudo code describing the loop; I am not near a machine with R) One way I can thing of is to create a loop counter variable, say lc<-1 and to increment it within the loop and then use a switch statement to execute the appropriate expression. This sems like a kludge to me. Is there a neater way? [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] List to Array: How to establish the dimension of the array
On 1/24/2012 2:47 PM, Ajay Askoolum wrote: Given a variable aa in the workspace, some of its attributes are: typeof(aa) [1] "list" mode(aa) [1] "list" length(aa) [1] 2 How do I retrieve the maximum indices, in this case 2,3,4? The variable itself is: aa [[1]] [[1]][[1]] [[1]][[1]][[1]] [1] 37531.52 [[1]][[1]][[2]] [1] 62787.32 [[1]][[1]][[3]] [1] 5503.184 [[1]][[1]][[4]] [1] 33832.8 [[1]][[2]] [[1]][[2]][[1]] [1] 20469.6 [[1]][[2]][[2]] [1] 27057.27 [[1]][[2]][[3]] [1] 51160.25 [[1]][[2]][[4]] [1] 45165.24 [[1]][[3]] [[1]][[3]][[1]] [1] 957.932 [[1]][[3]][[2]] [1] 21902.94 [[1]][[3]][[3]] [1] 37531.52 [[1]][[3]][[4]] [1] 62787.32 [[2]] [[2]][[1]] [[2]][[1]][[1]] [1] 5503.184 [[2]][[1]][[2]] [1] 33832.8 [[2]][[1]][[3]] [1] 20469.6 [[2]][[1]][[4]] [1] 27057.27 [[2]][[2]] [[2]][[2]][[1]] [1] 51160.25 [[2]][[2]][[2]] [1] 45165.24 [[2]][[2]][[3]] [1] 957.932 [[2]][[2]][[4]] [1] 21902.94 [[2]][[3]] [[2]][[3]][[1]] [1] 37531.52 [[2]][[3]][[2]] [1] 62787.32 [[2]][[3]][[3]] [1] 5503.184 [[2]][[3]][[4]] [1] 33832.8 [[alternative HTML version deleted]] Better to give the dput version of aa, so that it can be recreated easily aa <- list(list(list(37531.52, 62787.32, 5503.184, 33832.8), list(20469.60, 27057.27, 51160.25, 45165.24), list(957.932, 21902.94, 37531.52, 62787.32)), list(list(5503.184, 33832.8, 20469.6, 27057.27), list(51160.25, 45165.24, 957.932, 21902.94), list(37531.52, 62787.32, 5503.184, 33832.8))) Given that, the three "dimensions" can be gotten with length(aa) max(sapply(aa, length)) max(sapply(aa, sapply, length)) which give > length(aa) [1] 2 > max(sapply(aa, length)) [1] 3 > max(sapply(aa, sapply, length)) [1] 4 If you want to turn this into an actual array (assuming it is regular), you can do that fairly easily with the plyr package (I imagine you can do it with base functions too, but in a quick attempt was not able to do so). library("plyr") aaa <- laply(aa, laply, laply, identity) Then all the dimensions can be gotten at once > dim(aaa) [1] 2 3 4 -- Brian S. Diggs, PhD Senior Research Associate, Department of Surgery Oregon Health & Science University __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] List to Array: How to establish the dimension of the array
Given a variable aa in the workspace, some of its attributes are: > typeof(aa) [1] "list" > mode(aa) [1] "list" > length(aa) [1] 2 How do I retrieve the maximum indices, in this case 2,3,4? The variable itself is: > aa [[1]] [[1]][[1]] [[1]][[1]][[1]] [1] 37531.52 [[1]][[1]][[2]] [1] 62787.32 [[1]][[1]][[3]] [1] 5503.184 [[1]][[1]][[4]] [1] 33832.8 [[1]][[2]] [[1]][[2]][[1]] [1] 20469.6 [[1]][[2]][[2]] [1] 27057.27 [[1]][[2]][[3]] [1] 51160.25 [[1]][[2]][[4]] [1] 45165.24 [[1]][[3]] [[1]][[3]][[1]] [1] 957.932 [[1]][[3]][[2]] [1] 21902.94 [[1]][[3]][[3]] [1] 37531.52 [[1]][[3]][[4]] [1] 62787.32 [[2]] [[2]][[1]] [[2]][[1]][[1]] [1] 5503.184 [[2]][[1]][[2]] [1] 33832.8 [[2]][[1]][[3]] [1] 20469.6 [[2]][[1]][[4]] [1] 27057.27 [[2]][[2]] [[2]][[2]][[1]] [1] 51160.25 [[2]][[2]][[2]] [1] 45165.24 [[2]][[2]][[3]] [1] 957.932 [[2]][[2]][[4]] [1] 21902.94 [[2]][[3]] [[2]][[3]][[1]] [1] 37531.52 [[2]][[3]][[2]] [1] 62787.32 [[2]][[3]][[3]] [1] 5503.184 [[2]][[3]][[4]] [1] 33832.8 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.