search for: permutatematrix

Displaying 1 result from an estimated 1 matches for "permutatematrix".

2011 Mar 08
1
Replacing values in a data.frame/matrix
...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(c(2,1,3,1,2,3,3,2,1), ncol = 3, byrow=T) i.e. each 1 should map to 2 and vice verse while 3 maps to itself. What I've done so far is: permutateMatrix <- function(mat, perm=NULL) { values <- 1:NCOL(mat) if (is.null(perm)) { perm <- sample(values) } newmat <- replace(mat, sapply(values, function (val) which(mat==val)), rep(perm, each=NROW(mat))) return(list(mat.perm=newmat, perm=perm)) } "perm" is the p...