Hi, Suppose I have following dataset : mat <- matrix(rnorm(100), 50) Now I want to put 2nd column in the place of 1st and 1st column in the place of 2nd. Is there any "quick" way to do that? Thanks and regards, -- View this message in context: http://www.nabble.com/Altering-columns-tp24099597p24099597.html Sent from the R help mailing list archive at Nabble.com.
mat[ , 2:1] On Jun 18, 2009, at 4:14 PM, RON70 wrote:> > Hi, > > Suppose I have following dataset : > mat <- matrix(rnorm(100), 50) > > Now I want to put 2nd column in the place of 1st and 1st column in > the place > of 2nd. Is there any "quick" way to do that? > > Thanks and regards, > > -- > View this message in context: http://www.nabble.com/Altering-columns-tp24099597p24099597.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.David Winsemius, MD Heritage Laboratories West Hartford, CT
A simple way to do it would be: mat<-mat[,c(2,1)] Slightly more fancy (for any number of columns): mat<-mat[,dim(mat)[2]:1] I'm sure there are prettier ways to do it. -Jim On Jun 18, 2009, at 4:14 PM, RON70 wrote:> > Hi, > > Suppose I have following dataset : > mat <- matrix(rnorm(100), 50) > > Now I want to put 2nd column in the place of 1st and 1st column in > the place > of 2nd. Is there any "quick" way to do that? > > Thanks and regards, > > -- > View this message in context: http://www.nabble.com/Altering-columns-tp24099597p24099597.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.