[R] Vectorization in a random order
Richard M. Heiberger
rmh at temple.edu
Thu Nov 10 17:01:45 CET 2016
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))
More information about the R-help
mailing list