Re: [R] Replacing values in a data.frame/matrix

2011-03-08 Thread Thaler, Thorn, LAUSANNE, Applied Mathematics
n,LAUSANNE,Applied Mathematics > Cc: r-help@r-project.org > Subject: Re: [R] Replacing values in a data.frame/matrix > > how about: > > m <- matrix(c(1,2,3,2,1,3,3,1,2), ncol = 3, byrow = TRUE) > perm <- c(1, 3, 2) > > out <- perm[m] > dim(out) <- dim(m)

Re: [R] Replacing values in a data.frame/matrix

2011-03-08 Thread Dimitris Rizopoulos
how about: m <- matrix(c(1,2,3,2,1,3,3,1,2), ncol = 3, byrow = TRUE) perm <- c(1, 3, 2) out <- perm[m] dim(out) <- dim(m) out I hope it helps. Best, Dimitris On 3/8/2011 4:05 PM, Thaler, Thorn, LAUSANNE, Applied Mathematics wrote: Hi all, Suppose we have the following matrix m<- matrix(c

[R] Replacing values in a data.frame/matrix

2011-03-08 Thread Thaler, Thorn, LAUSANNE, Applied Mathematics
Hi all, Suppose we have the following matrix m <- matrix(c(1,2,3,2,1,3,3,1,2), ncol = 3, byrow=T) where in each row each number occurs only once. I'd like to define a permutation, e.g. 1 -> 2, 2 -> 1, 3 -> 3 and apply it to the matrix. Thus, the following matrix should result: m.perm <- matrix