Ajay Askoolum
2012-Jan-24 22:47 UTC
[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]]
Brian Diggs
2012-Jan-24 23:34 UTC
[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