[R] sort a 3 dimensional array across third dimension ?

rex.dwyer at syngenta.com rex.dwyer at syngenta.com
Fri Feb 18 15:57:24 CET 2011


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. 


More information about the R-help mailing list