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

Maas James Dr (MED) J.Maas at uea.ac.uk
Fri Feb 18 16:05:47 CET 2011


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
>



More information about the R-help mailing list