[R] permutates and/or samples a matrix

David Winsemius dwinsemius at comcast.net
Thu Jan 31 14:44:46 CET 2008


On 31 Jan 2008, you wrote in gmane.comp.lang.r.general:

> Hi,
> 
> sample(x) only permutates and/or samples from a vector, which I
> can't use for a matrix. Please help.

 
You just feed sample(.) a "redimensioned" object. If you need to do it 
programmatically just multiply the dimensions of the matrix to create a 
length for the working vector.

> xm<-rnorm(25)
> xmtx<-matrix(xm,dim<-c(5,5))
> samp<-sample(as.matrix(xmtx,dim<-c(1,25)))
> samp
#output of 25 numbers elided

> samp<-sample(as.matrix(xmtx,dim<-c(1,25)),10)
> samp
 [1]  0.26421745  0.63888288  1.06176705 -1.56074510 -0.72471549 -
0.38743020
 [7] -0.68765683  1.18081648 -1.51566762  0.07560708

-- 
David Winsemius



More information about the R-help mailing list