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.
I think you answered your own question. For loops are not a boogeyman... poor memory management is. Algorithms that are sensitive to evaluation sequence are often not very re-usable, and certainly not parallelizable. If you have a specific algorithm in mind, there may be some advice we can give you about optimization, but as it stands think you know how to get a working implementation. -- Sent from my phone. Please excuse my brevity. On November 10, 2016 5:06:07 AM PST, 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.
nBuyMat <- data.frame(matrix(rnorm(28), 7, 4)) nBuyMat nBuy <- nrow(nBuyMat) sample(1:nBuy, nBuy, replace=FALSE) sample(1:nBuy) sample(nBuy) ?sample apply(nBuyMat[sample(1:nBuy,nBuy, replace=FALSE),], 1, function(x) sum(x)) apply(nBuyMat[sample(nBuy),], 1, function(x) sum(x)) The defaults for sample do what you have requested. If the original row identification matters, then be sure the to use either a matrix with rownames or a data.frame. Rich Sent from my iPhone> On Nov 10, 2016, at 08:06, Thomas Chesney <Thomas.Chesney at nottingham.ac.uk> wrote: > > for (b in sample(1:nBuy,nBuy, replace=FALSE)){ > > } > > but > > apply(nBuyMat, 1, function(x))
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.