Is there an idiomatic way of swapping rows/cols in a Matrix? The standard approach for swapping rows r and u: tmpRow = A[r,] A[r,] = A[u,] A[u,] = tmpRow Robin Hankin (R-Octave help sheet maintainer) says: [I would implement your three line solution (which works!) as follows: swap <- function(a,m,n) { tmp <- a[m] a[m] <- a[n] a[n] <- tmp return(a) } then use things like x <- matrix(1:100,10,10) x[ swap(1:10,5,6),] #swaps rows 5&6 x[,swap(1:10,5,6) ] #swaps cols 5&6 x[ swap(1:10,5,6),swap(1:10,5,6)] #both together ] Anybody? -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Uwe Ligges
2002-Sep-17 19:57 UTC
[R] Common R/S Idioms - Swapping Rows or Cols in a Matrix
Rob Lee wrote:> > Is there an idiomatic way of swapping rows/cols in a Matrix? > > The standard approach for swapping rows r and u: > tmpRow = A[r,] > A[r,] = A[u,] > A[u,] = tmpRow > > Robin Hankin (R-Octave help sheet maintainer) says: > > [I would implement your three line solution (which works!) as follows: > > swap <- function(a,m,n) { > tmp <- a[m] > a[m] <- a[n] > a[n] <- tmp > return(a) > } > > then use things like > > x <- matrix(1:100,10,10) > > x[ swap(1:10,5,6),] #swaps rows 5&6 > x[,swap(1:10,5,6) ] #swaps cols 5&6 > x[ swap(1:10,5,6),swap(1:10,5,6)] #both together > > ] > > Anybody?What about vectorized indexing as common in S? For example swapping rows m and n of a matrix A: A[c(m, n), ] <- A [c(n, m), ] Uwe Ligges -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Giovanni Petris
2002-Sep-17 22:27 UTC
[R] Common R/S Idioms - Swapping Rows or Cols in a Matrix
I would try something like> A[c(s,r),] <- A[c(r,s),]Giovanni -- __________________________________________________ [ ] [ Giovanni Petris GPetris at uark.edu ] [ Department of Mathematical Sciences ] [ University of Arkansas - Fayetteville, AR 72701 ] [ Ph: (479) 575-6324, 575-8630 (fax) ] [ http://definetti.uark.edu/~gpetris/ ] [__________________________________________________]> Is there an idiomatic way of swapping rows/cols in a Matrix? > > The standard approach for swapping rows r and u: > tmpRow = A[r,] > A[r,] = A[u,] > A[u,] = tmpRow > > Robin Hankin (R-Octave help sheet maintainer) says: > > [I would implement your three line solution (which works!) as follows: > > swap <- function(a,m,n) { > tmp <- a[m] > a[m] <- a[n] > a[n] <- tmp > return(a) > } > > then use things like > > x <- matrix(1:100,10,10) > > x[ swap(1:10,5,6),] #swaps rows 5&6 > x[,swap(1:10,5,6) ] #swaps cols 5&6 > x[ swap(1:10,5,6),swap(1:10,5,6)] #both together > > ] > > Anybody?-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._