# [R] Vectorization in a random order

Bert Gunter bgunter.4567 at gmail.com
Thu Nov 10 17:27:32 CET 2016

```You are mistaken. apply() is *not* vectorized. It is a disguised loop.

For true vectorization at the C level, the answer must be no, as the
whole point is to treat the argument as a whole object and hide the
iterative details.

However, as you indicated, you can always manually randomize the
indexing that is being iterated over and even write a function to do
it if you like; e.g. (warning: esentially untested and probably clumsy
as well as buggy)

randapply <- function(X, MARGIN, FUN,...)
{
d <- dim(X)
ix <- as.list(rep(TRUE,length(d)))
for(i in MARGIN) ix[[i]] <- sample(seq_len(d[i]),d[i])
X <- do.call("[", c(list(X), ix))
apply(X,MARGIN,FUN,...)
}

> a <- array(1:24,dim = 2:4)

> randapply(a, 3,mean)
[1]  9.5 21.5 15.5  3.5

> randapply(a,3,mean)
[1] 21.5  3.5 15.5  9.5

Cheers,
Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Thu, Nov 10, 2016 at 5:06 AM, Thomas Chesney
<Thomas.Chesney at nottingham.ac.uk> wrote:
> Is there a way to use vectorization where the elements are evaluated in a random order?
>
> For instance, if the code is to be run on each row in a matrix of length nBuy the following will do the job
>
> for (b in sample(1:nBuy,nBuy, replace=FALSE)){
>
> }
>
> but
>
> apply(nBuyMat, 1, function(x))
>
> will be run I believe, in the same order each time (Row1, then Row2, then Row3 etc.)
>
> This is important for building agent based models (the classic explanation of this is probably Huberman & Glance's response to Nowak & May's 1992 Nature article - Evolutionary games and computer simulations, http://www.pnas.org/content/90/16/7716.abstract)
>
> Thank you,
>
> Thomas
> http://www.nottingham.ac.uk/~liztc/Personal/index.html
>
>
>
> This message and any attachment are intended solely for the addressee
> and may contain confidential information. If you have received this
> message in error, please send it back to me, and immediately delete it.
>
> Please do not use, copy or disclose the information contained in this
> message or in any attachment.  Any views or opinions expressed by the
> author of this email do not necessarily reflect the views of the
> University of Nottingham.
>
> This message has been checked for viruses but the contents of an
> attachment may still contain software viruses which could damage your
> computer system, you are advised to perform your own checks. Email
> communications with the University of Nottingham may be monitored as
> permitted by UK legislation.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

```

More information about the R-help mailing list