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

rex.dwyer at syngenta.com rex.dwyer at syngenta.com
Fri Feb 18 17:03:49 CET 2011


I was going to say:

The problem with for-loops (as best I understand it) is that the R code gets interpreted over and over; what you normally want to do is design the computation so that you jump into the internals of R and stay there.  But the inner loop is in the R internals of the sort in this case. If the third dimension is even just moderately large, the cost of interpretation is small relative to the cost of the sort.

But my quick experiments don't exactly bear that out:

> foo = runif(10000)
> system.time(for (i in 1:1000) sort(foo))
   user  system elapsed
   1.60    0.00    1.61
> system.time(for (i in 1:1000) for (j in 1:10000) k=k+1)
   user  system elapsed
   7.52    0.00    7.54

I imagine I could find a prettier way with various flavors of apply, if my employer didn't have other things for me to do.

Maybe someone else can explain why the for loop is so slow that the overhead to increment the index is greater than sorting 10000 doubles.  I know it used to be even slower in splus than in R.


-----Original Message-----
From: Maas James Dr (MED) [mailto:J.Maas at uea.ac.uk]
Sent: Friday, February 18, 2011 10:06 AM
To: Dwyer Rex USRE; r-help at r-project.org
Subject: RE: 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
>




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