Maas James Dr (MED)
2011-Feb-18 13:01 UTC
[R] sort a 3 dimensional array across third dimension ?
I'm attempting to sort a 3 dimensional array that looks like this> x, , 1 [,1] [,2] [1,] 9 9 [2,] 7 9 , , 2 [,1] [,2] [1,] 6 5 [2,] 4 6 , , 3 [,1] [,2] [1,] 2 1 [2,] 3 2 Such that it ends up like this ....> y, , 1 [,1] [,2] [1,] 2 1 [2,] 3 2 , , 2 [,1] [,2] [1,] 6 5 [2,] 4 6 , , 3 [,1] [,2] [1,] 9 9 [2,] 7 9 I think this is sorting across the third dimension but several attempts using either the sort or apply functions have not worked. Any and all suggestions most welcome. Thanks J ==============================Dr. Jim Maas University of East Anglia [[alternative HTML version deleted]]
Joshua Wiley
2011-Feb-18 13:16 UTC
[R] sort a 3 dimensional array across third dimension ?
Dear Jim, This works for your small example. I was not exactly sure what you were sorting on. In my example, I sum all values at each level of the third dimension and sort by that sum. Of course, if there are many levels or the 2 dimensions being summed are quite large, this will be quite computationally inefficient. I also show how you could sort but just the value in cell [1, 1, i] where i moves across all levels of the third dimension. ## Your data x <- array(c(9, 9, 7, 9, 6, 5, 4, 6, 2, 1, 3, 2), dim = list(2, 2, 3)) ## sorting by sum dims 1&2 at each level of 3rd x[ , , order(apply(x, 3, sum))] ## sorting by cell [1, 1] at each level of 3rd x[ , , order(x[1, 1, ])] HTH, Josh On Fri, Feb 18, 2011 at 5:01 AM, Maas James Dr (MED) <J.Maas at uea.ac.uk> wrote:> I'm attempting to sort a 3 dimensional array that looks like this >> x > , , 1 > ? ? [,1] [,2] > [1,] ? ?9 ? ?9 > [2,] ? ?7 ? ?9 > , , 2 > ? ? [,1] [,2] > [1,] ? ?6 ? ?5 > [2,] ? ?4 ? ?6 > , , 3 > ? ? [,1] [,2] > [1,] ? ?2 ? ?1 > [2,] ? ?3 ? ?2 > > Such that it ends up like this .... >> y > , , 1 > ? ? [,1] [,2] > [1,] ? ?2 ? ?1 > [2,] ? ?3 ? ?2 > , , 2 > ? ? [,1] [,2] > [1,] ? ?6 ? ?5 > [2,] ? ?4 ? ?6 > , , 3 > ? ? [,1] [,2] > [1,] ? ?9 ? ?9 > [2,] ? ?7 ? ?9 > > I think this is sorting across the third dimension but several attempts using either the sort or apply functions have not worked. ?Any and all suggestions most welcome. ?Thanks > > J > > ==============================> Dr. Jim Maas > University of East Anglia > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Claudia Beleites
2011-Feb-18 14:30 UTC
[R] sort a 3 dimensional array across third dimension ?
Dear James, this is what I understood your sorting along the third dimension to be: > x <- array(c(9, 9, 7, 9, 6, 5, 4, 6, 2, 1, 3, 2), dim = list(2, 2, 3)) > y <- apply (x, 1:2, sort) > y , , 1 [,1] [,2] [1,] 2 1 [2,] 6 5 [3,] 9 9 , , 2 [,1] [,2] [1,] 3 2 [2,] 4 6 [3,] 7 9 The results of apply are length (result of function) x [shape of x without the dimensions you hand to apply). Thus, your specified result needs rearranging the dimensions: > y <- aperm (y, c(2, 3, 1)) > y , , 1 [,1] [,2] [1,] 2 3 [2,] 1 2 , , 2 [,1] [,2] [1,] 6 4 [2,] 5 6 , , 3 [,1] [,2] [1,] 9 7 [2,] 9 9 HTH Claudia -- Claudia Beleites Dipartimento dei Materiali e delle Risorse Naturali Universit? degli Studi di Trieste Via Alfonso Valerio 6/a I-34127 Trieste phone: +39 0 40 5 58-37 68 email: cbeleites at units.it
rex.dwyer at syngenta.com
2011-Feb-18 14:57 UTC
[R] sort a 3 dimensional array across third dimension ?
Although I suggested to someone else that for-loops be avoided, they are not in the inner loop in this code, and it's probably easier to understand than some sort of apply: a = array(round(100*runif(60)),dim=c(3,4,5)) a for (i in 1:dim(a)[1]) for (j in 1:dim(a)[2]) a[i,j,] = sort(a[i,j,]) a Is that what you want? -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Maas James Dr (MED) Sent: Friday, February 18, 2011 8:01 AM To: r-help at r-project.org Subject: [R] sort a 3 dimensional array across third dimension ? I'm attempting to sort a 3 dimensional array that looks like this> x, , 1 [,1] [,2] [1,] 9 9 [2,] 7 9 , , 2 [,1] [,2] [1,] 6 5 [2,] 4 6 , , 3 [,1] [,2] [1,] 2 1 [2,] 3 2 Such that it ends up like this ....> y, , 1 [,1] [,2] [1,] 2 1 [2,] 3 2 , , 2 [,1] [,2] [1,] 6 5 [2,] 4 6 , , 3 [,1] [,2] [1,] 9 9 [2,] 7 9 I think this is sorting across the third dimension but several attempts using either the sort or apply functions have not worked. Any and all suggestions most welcome. Thanks J ==============================Dr. Jim Maas University of East Anglia [[alternative HTML version deleted]] ______________________________________________ R-help at 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. message may contain confidential information. If you are not the designated recipient, please notify the sender immediately, and delete the original and any copies. Any use of the message by you is prohibited.
Maas James Dr (MED)
2011-Feb-18 15:05 UTC
[R] sort a 3 dimensional array across third dimension ?
Hi Rex, Thanks, this is exactly what I want but have to do it with many big arrays ... thus if there were a way to do it with a vectorized function would it not be a lot more efficient? Much appreciated! J>Subject: RE: sort a 3 dimensional array across third dimension ? > >Although I suggested to someone else that for-loops be avoided, they are >not in the inner loop in this code, and it's probably easier to >understand than some sort of apply: > >a = array(round(100*runif(60)),dim=c(3,4,5)) >a >for (i in 1:dim(a)[1]) > for (j in 1:dim(a)[2]) > a[i,j,] = sort(a[i,j,]) >a > >Is that what you want? > >Subject: [R] sort a 3 dimensional array across third dimension ? > >I'm attempting to sort a 3 dimensional array that looks like this >> x >, , 1 > [,1] [,2] >[1,] 9 9 >[2,] 7 9 >, , 2 > [,1] [,2] >[1,] 6 5 >[2,] 4 6 >, , 3 > [,1] [,2] >[1,] 2 1 >[2,] 3 2 > >Such that it ends up like this .... >> y >, , 1 > [,1] [,2] >[1,] 2 1 >[2,] 3 2 >, , 2 > [,1] [,2] >[1,] 6 5 >[2,] 4 6 >, , 3 > [,1] [,2] >[1,] 9 9 >[2,] 7 9 > >I think this is sorting across the third dimension but several attempts >using either the sort or apply functions have not worked. Any and all >suggestions most welcome. Thanks > >J > >==============================>Dr. Jim Maas >University of East Anglia >
Claudia Beleites
2011-Feb-18 16:02 UTC
[R] {Spam?} Re: sort a 3 dimensional array across third dimension ?
On 02/18/2011 04:11 PM, Maas James Dr (MED) wrote:> Hi Claudia, > > It does help a lot, but not quite there yet ... I'm sure you are correct and > is much appreciated, I need some sort of generalized form, actual arrays in > my case are 3x3x1000. Do you suspect it could be done in one step with > sapply?Why sapply? Sure you can to it in one step: y<- aperm (apply (x, 1:2, sort), c(2, 3, 1)) I just think two lines are more readable. Note that all these numbers are the "directions" of the array and don't have anything to do with the actual size. Just try it out with different array sizes. > a <- array (runif (9000), c (3, 3, 1000)) > a [,,1:2] , , 1 [,1] [,2] [,3] [1,] 0.8721 0.5102 0.47370 [2,] 0.7721 0.5744 0.98281 [3,] 0.9357 0.1969 0.08784 , , 2 [,1] [,2] [,3] [1,] 0.1485 0.6878 0.1018 [2,] 0.3784 0.3864 0.9814 [3,] 0.9219 0.5664 0.4565 > y<- aperm (apply (a, 1:2, sort), c(2, 3, 1)) > y [,,1:2] , , 1 [,1] [,2] [,3] [1,] 1.121e-03 1.517e-03 0.0008285 [2,] 7.118e-05 3.303e-04 0.0003870 [3,] 7.445e-04 2.461e-05 0.0005980 , , 2 [,1] [,2] [,3] [1,] 0.001375 0.0049272 0.004581 [2,] 0.002204 0.0004947 0.001148 [3,] 0.004214 0.0006355 0.001610 > y [,,999:1000] , , 1 [,1] [,2] [,3] [1,] 0.9989 0.9980 0.9998 [2,] 0.9982 0.9973 0.9994 [3,] 0.9994 0.9978 0.9993 , , 2 [,1] [,2] [,3] [1,] 0.9997 0.9992 0.9999 [2,] 0.9986 0.9981 0.9997 [3,] 0.9998 0.9988 0.9996 BTW: as your MARGINS are short, only 3 x 3 = 9 calls to FUN are necessary. I don't think you can gain much time here. The calculation with 3 x 3 x 1000 on my computer had 3 ms elapsed, and increasing every direction by a factor of 10 still needs 1/3 s. Claudia> > Regards > > J > > > =============================== Dr. Jim Maas Research Associate in Network > Meta-Analysis School of Medicine, Health Policy and Practice CD Annex, Room > 1.04 University of East Anglia Norwich, UK NR4 7TJ > > +44 (0) 1603 591412 > > >> From: Claudia Beleites [mailto:cbeleites at units.it] >> >> Dear James, >> >> this is what I understood your sorting along the third dimension to be: >>> x<- array(c(9, 9, 7, 9, 6, 5, 4, 6, 2, 1, 3, 2), dim = list(2, 2, >> 3)) >> >>> y<- apply (x, 1:2, sort) y >> , , 1 >> >> [,1] [,2] [1,] 2 1 [2,] 6 5 [3,] 9 9 >> >> , , 2 >> >> [,1] [,2] [1,] 3 2 [2,] 4 6 [3,] 7 9 >> >> >> The results of apply are length (result of function) x [shape of x without >> the dimensions you hand to apply). >> >> Thus, your specified result needs rearranging the dimensions: >> >>> y<- aperm (y, c(2, 3, 1)) y >> , , 1 >> >> [,1] [,2] [1,] 2 3 [2,] 1 2 >> >> , , 2 >> >> [,1] [,2] [1,] 6 4 [2,] 5 6 >> >> , , 3 >> >> [,1] [,2] [1,] 9 7 [2,] 9 9 >> >> >> HTH Claudia >> >> -- Claudia Beleites Dipartimento dei Materiali e delle Risorse Naturali >> Universit? degli Studi di Trieste Via Alfonso Valerio 6/a I-34127 Trieste >> >> phone: +39 0 40 5 58-37 68 email: cbeleites at units.it-- Claudia Beleites Dipartimento dei Materiali e delle Risorse Naturali Universit? degli Studi di Trieste Via Alfonso Valerio 6/a I-34127 Trieste phone: +39 0 40 5 58-37 68 email: cbeleites at units.it
Thanks Claudia (and all others!0 This works beautifully! The instructions for aperm are a bit vauge! J -- View this message in context: http://r.789695.n4.nabble.com/sort-a-3-dimensional-array-across-third-dimension-tp3312612p3313126.html Sent from the R help mailing list archive at Nabble.com.