Dear all
I've got stuck when trying to replace values in a vector by selecting
replacements from a replacement table. I'm trying to use only base
functions. Here's a dummy example:
(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"
[23] "w" "x" "y" "z" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l"
"m" "n" "o" "p" "q" "r"
[45] "s" "t" "u" "v" "w" "x" "y" "z"
values repl
[1,] "aa" "aa"
[2,] "a" "A"
[3,] "b" "B"
[4,] NA NA
[5,] "d" "D"
[6,] "zz" "zz"
Now I can easily compute all four combinations of 'match' and '%in%':
(ind <- match(x, repl.tab[ ,1]))
[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
[30] 5 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[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"
On Mon, Jul 30, 2012 at 11:53 AM, Liviu Andronic <landronimirc at gmail.com> wrote:
Dear all
I've got stuck when trying to replace values in a vector by selecting
replacements from a replacement table. I'm trying to use only base
functions. Here's a dummy example:
(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"
[23] "w" "x" "y" "z" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l"
"m" "n" "o" "p" "q" "r"
[45] "s" "t" "u" "v" "w" "x" "y" "z"
values repl
[1,] "aa" "aa"
[2,] "a" "A"
[3,] "b" "B"
[4,] NA NA
[5,] "d" "D"
[6,] "zz" "zz"
Now I can easily compute all four combinations of 'match' and '%in%':
(ind <- match(x, repl.tab[ ,1]))
[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
[30] 5 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[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.
(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"
[,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