[R] Swap rows and columns in a matrix

David Winsemius dwinsemius at comcast.net
Wed Oct 31 04:48:29 CET 2012


On Oct 30, 2012, at 5:34 PM, arun wrote:

> HI David,
> 
> Seems like a closing bracket is needed.
> 
> mat[, sample(dim(mat)[2]) ]
> 

Actually a closing paren:

set.seed(123)
mat[, sample(dim(mat) ) [2] ]

>                    ^^^                    ^^^
> A.K.
> 
> 
> 
> 
> ----- Original Message -----
> From: David Winsemius <dwinsemius at comcast.net>
> To: Haris Rhrlp <haris_r_help at yahoo.com>
> Cc: "R-help at r-project.org" <R-help at r-project.org>
> Sent: Tuesday, October 30, 2012 6:51 PM
> Subject: Re: [R] Swap rows and columns in a matrix
> 
> 
> On Oct 30, 2012, at 11:59 AM, Haris Rhrlp wrote:
> 
>> Dear R users,
>> 
>> I want a help to write an algorithm for swapping rows and columns in a matrix
>> 
> 
> This will "shuffle" columns, although 'randomly permute' is the more common word for this;operation:
> 
> set.seed(123)
> mat[, sample(dim(mat)[2] ]
> 
> This will swap rows:
> 
> set.seed(123)
> mat[ sample(dim(mat)[1],  ]
> 
> -- 
> David Winsemius, MD
> Alameda, CA, USA
> 
> ______________________________________________
> 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.
> 

David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list