[R] permutates and/or samples a matrix

David Winsemius dwinsemius at comcast.net
Thu Jan 31 16:16:20 CET 2008


David Winsemius <dwinsemius at comcast.net> wrote : 

> On 31 Jan 2008, Stanley wrote:

>> 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
> 

Looking at Csardi's reply made me realize I may have incorrectly 
characterized your experience. I thought you said you had been unable to 
use sample() from a matrix. Sample appears to return elements from a matrix 
without complaint without the redimensioning step. Sorry, I must have 
misunderstood your question.

-- 
David Winsemius



More information about the R-help mailing list