Suppose your matrix is called X.
? sample
X[sample(nrow(X)),]
Michael
On Wed, Nov 16, 2011 at 11:45 AM, Juan Antonio Balbuena <balbuena at
uv.es> wrote:> Hello
> This is probably a basic question but I am quite new to R.
>
> I need to permute elements within rows of a binary matrix, such as
>
> ? ? [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
> ?[1,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?1 ? ?0 ? ?0 ? ?0 ? ?0 ? ? 0
> ?[2,] ? ?0 ? ?0 ? ?1 ? ?1 ? ?0 ? ?0 ? ?0 ? ?1 ? ?1 ? ? 0
> ?[3,] ? ?0 ? ?1 ? ?0 ? ?0 ? ?0 ? ?0 ? ?1 ? ?0 ? ?0 ? ? 0
> ?[4,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?1 ? ?1 ? ?0 ? ? 0
> ?[5,] ? ?0 ? ?0 ? ?0 ? ?1 ? ?0 ? ?0 ? ?0 ? ?0 ? ?1 ? ? 0
> ?[6,] ? ?0 ? ?0 ? ?1 ? ?1 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ? 1
> ?[7,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ? 0
> ?[8,] ? ?1 ? ?1 ? ?0 ? ?1 ? ?0 ? ?0 ? ?0 ? ?1 ? ?0 ? ? 1
> ?[9,] ? ?1 ? ?0 ? ?0 ? ?1 ? ?0 ? ?1 ? ?0 ? ?1 ? ?0 ? ? 0
> [10,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?1 ? ?0 ? ? 1
>
>
> That is, elements within each row are permuted freely and independently
from
> the other rows.
>
> I see that is is workable by creating a array for each row, performing
> sample and binding the arrays again, but I wonder whether there is a more
> efficient way of doing the trick.
>
> Any help will be much appreciated.
>
>
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/permutation-within-rows-of-a-matrix-tp4076989p4076989.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>