Re: [R] Vectorization in a random order

2016-11-10 Thread Jeff Newmiller
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

Re: [R] Vectorization in a random order

2016-11-10 Thread Bert Gunter
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 index

Re: [R] Vectorization in a random order

2016-11-10 Thread Richard M. Heiberger
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 sa

[R] Vectorization in a random order

2016-11-10 Thread Thomas Chesney
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