[R] random sampling matrix

Daniel Nordlund djnordlund at frontier.com
Wed Dec 19 08:56:31 CET 2012


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of ???
> Sent: Tuesday, December 18, 2012 9:08 PM
> To: r-help at r-project.org
> Subject: [R] random sampling matrix
> 
> Hello
> 
> 
> 
> I have a one question about random sampling matrix
> 
> 
> 
> I want to regeneration value of matrix
> 
> For example,
> 
> Matrix A :
> 
> 1  2  3
> 
> 11 12 13
> 
> 21 22 23 .
> 
> > sample= data.frame(a[sample(1:dim(a)[1]),sample(1:dim(a)[2])])
> 
> Then,
> 
> Matrix sample :
> 
> 21 23 22
> 
> 11 13 12
> 
> 1  3  2
> 
> 
> 
> But, I want to regeneration.
> 
> Ex) Matrix sample
> 
> 1      23  2
> 
> 22  11  3
> 
> 12  21  13
> 
> 
> 
> Pleases kindly help with R code!&
> 
> 
> 
> Thank you.
> 

Does this do what you want?

> m <- matrix(c(1,21,31,2,22,23,3,23,33), nrow=3)
> m
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]   21   22   23
[3,]   31   23   33
> matrix(sample(m),nrow=3)
     [,1] [,2] [,3]
[1,]   33   23    1
[2,]    2    3   22
[3,]   31   23   21
>  matrix(sample(m),nrow=3)
     [,1] [,2] [,3]
[1,]   21   23    1
[2,]   31   33   23
[3,]    2    3   22
>  matrix(sample(m),nrow=3)
     [,1] [,2] [,3]
[1,]   31    2   21
[2,]   33    1   22
[3,]   23   23    3
>

Hope this is helpful,

Dan

Daniel Nordlund
Bothell, WA USA




More information about the R-help mailing list