Attachment 'replaceNameMatches.R'
Download 1 #' Replace Vector Names from a Lookup Vector
2 #'
3 #' Replace vector names using a named vector that is used as a look up table.
4 #' @param x A frequency vector or another vector with \code{"names"} attribute set.
5 #' @param lookup A named character vector.
6 #' @return If a name in \code{"x"} matches a value in \code{"lookup"}, it is replaced from the name of the matched item in \code{"lookup"}.
7 #' @examples
8 #' punctNames <- setNames(c("\t", "\n", "\r", "\u00a0", "\u2028", "\u2029"),
9 #' c("tab", "lf", "cr", "nbsp", "line", "para"))
10 #' hits <- setNames(1:5, c("a", "b", "\t", "c", "\u00a0"))
11 #' replaceNameMatches(hits, punctNames)
12 #' @export
13 replaceNameMatches <- function(x, lookup)
14 {
15 hits <- match(names(x), lookup, nomatch = 0)
16 for (i in 1:length(x))
17 if (hits[i] > 0)
18 names(x)[i] <- names(lookup)[hits[i]]
19 return(x)
20 }
You are not allowed to attach a file to this page.