replace values in vector from a replacement table
On Mon, Jul 30, 2012 at 6:00 PM, jim holtman <jholtman at gmail.com> wrote:
try this:
(x <- rep(letters,2))
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" [24] "x" "y" "z" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" [47] "u" "v" "w" "x" "y" "z"
values <- c("aa", "a", "b", NA, "d", "zz")
repl <- c("aa", "A", "B", NA, "D", "zz")
(repl.tab <- cbind(values, repl))
values repl [1,] "aa" "aa" [2,] "a" "A" [3,] "b" "B" [4,] NA NA [5,] "d" "D" [6,] "zz" "zz"
indx <- match(x, repl.tab[, 1], nomatch = 0) x[indx != 0] <- repl.tab[indx, 2] x
[1] "A" "B" "c" "D" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" [24] "x" "y" "z" "A" "B" "c" "D" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" [47] "u" "v" "w" "x" "y" "z"
Based on this code I came up with the following function.
replace2 <- function(x, ind, repl){
if(any(is.na(ind))) ind[is.na(ind)] <- 0
if(is.vector(x) & is.vector(repl)) {
(x[ind != 0] <- repl[ind])
return(x)
} else if(identical(ncol(x), ncol(repl))){
(x[ind != 0, ] <- repl[ind, ])
return(x)
}
}
Whereas replicate() can be used only on vectors of same dimension,
replicate2() can be used on vectors and matrices/dataframes, and the
replacement data can have different nr of rows. It also works with
index vectors containing NAs.
##for vectors (indx <- match(x, repl.tab[, 1], nomatch = 0))
[1] 2 3 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 3 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [46] 0 0 0 0 0 0 0
head(replace2(x, indx, repl.tab[, 2]))
[1] "A" "B" "c" "D" "e" "f"
(indx <- match(x, repl.tab[, 1])) ##index vector with NAs
[1] 2 3 NA 5 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 2 3 NA 5 [31] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
head(replace2(x, indx, repl.tab[, 2]))
[1] "A" "B" "c" "D" "e" "f"
##for matrices/dataframes head(xx <- cbind(x, x))
x x [1,] "a" "a" [2,] "b" "b" [3,] "c" "c" [4,] "d" "d" [5,] "e" "e" [6,] "f" "f"
(repl.tab2 <- cbind(repl.tab[, 2], repl.tab[, 2]))
[,1] [,2] [1,] "aa" "aa" [2,] "A" "A" [3,] "B" "B" [4,] NA NA [5,] "D" "D" [6,] "zz" "zz"
head(replace2(xx, indx, repl.tab2))
x x [1,] "A" "A" [2,] "B" "B" [3,] "c" "c" [4,] "D" "D" [5,] "e" "e" [6,] "f" "f" Does this function have any generic value? Are there obvious implementation mistakes? Regards Liviu