A <- matrix(rnorm(16)4,4)
o <- c(1,3,2,4)
A[o,o]

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
On Behalf Of Zhang Yanwei - Princeton-MRAm
Sent: Thursday, August 07, 2008 10:31 AM
To: r-help@r-project.org
Subject: [R] Switch two rows in a matrix


Hi all,
  I have a 4 by 4 matrix, and I want to switch row 2 and row 3 first,
then switch column 2 and column 3. Is there an easy way to do it?
The following is a tedious way to get what I want. But I wonder if there
is a way to simplify this.

 > a=matrix(rnorm(16),4,4)
> a
            [,1]       [,2]        [,3]       [,4]
[1,]  0.33833811 -0.9422273 -0.06181611 -1.8346134
[2,] -0.68167996  0.2004836  0.71079887 -1.1590184
[3,]  0.07811688  0.1338694 -1.61262688  0.2988365
[4,]  0.21869786 -1.6356661 -0.13694344 -1.2121355
> b=a
> b[2,]=a[3,]
> b[3,]=a[2,]
> b
            [,1]       [,2]        [,3]       [,4]
[1,]  0.33833811 -0.9422273 -0.06181611 -1.8346134
[2,]  0.07811688  0.1338694 -1.61262688  0.2988365
[3,] -0.68167996  0.2004836  0.71079887 -1.1590184
[4,]  0.21869786 -1.6356661 -0.13694344 -1.2121355
> c=b
> c[,2]=b[,3]
> c[,3]=b[,2]
> c
            [,1]        [,2]       [,3]       [,4]
[1,]  0.33833811 -0.06181611 -0.9422273 -1.8346134
[2,]  0.07811688 -1.61262688  0.1338694  0.2988365
[3,] -0.68167996  0.71079887  0.2004836 -1.1590184
[4,]  0.21869786 -0.13694344 -1.6356661 -1.2121355


Sincerely,
Yanwei Zhang
Department of Actuarial Research and Modeling
Munich Re America
Tel: 609-275-2176
Email: [EMAIL PROTECTED]<mailto:[EMAIL PROTECTED]>


        [[alternative HTML version deleted]]

______________________________________________
R-help@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.
--------------------------------------------------------

This message w/attachments (message) may be privileged, confidential or 
proprietary, and if you are not an intended recipient, please notify the 
sender, do not use or share it and delete it. Unless specifically indicated, 
this message is not an offer to sell or a solicitation of any investment 
products or other financial product or service, an official confirmation of any 
transaction, or an official statement of Merrill Lynch. Subject to applicable 
law, Merrill Lynch may monitor, review and retain e-communications (EC) 
traveling through its networks/systems. The laws of the country of each 
sender/recipient may impact the handling of EC, and EC may be archived, 
supervised and produced in countries other than the country in which you are 
located. This message cannot be guaranteed to be secure or error-free. This 
message is subject to terms available at the following link: 
http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch you 
consent to the foregoing.

______________________________________________
R-help@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.

Reply via email to