File: mapValues.R

package info (click to toggle)
r-cran-bbmisc 1.13.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,256 kB
  • sloc: ansic: 176; sh: 9; makefile: 5
file content (55 lines) | stat: -rw-r--r-- 1,852 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#' @title Replace values in atomic vectors
#'
#' @details
#' Replaces values specified in \code{from} with values in \code{to}.
#' Regular expression matching can be enabled which calls \code{\link[base]{gsub}} iteratively
#' on \code{x} to replace all patterns in \code{from} with replacements in \code{to}.
#'
#' @param x [\code{atomic}]\cr
#' Atomic vector. If \code{x} is a factor, all replacements work on the levels.
#' @param from [\code{atomic}]\cr
#' Atomic vector with values to replace, same length as \code{to}.
#' @param to [\code{atomic}]\cr
#' Atomic vector with replacements, same length as \code{from}.
#' @param regex [\code{logical}]\cr
#' Use regular expression matching? Default is \code{FALSE}.
#' @param ignore.case [\code{logical}]\cr
#' Argument passed to \code{\link[base]{gsub}}.
#' @param perl [\code{logical}]\cr
#' Argument passed to \code{\link[base]{gsub}}.
#' @param fixed [\code{logical}]\cr
#' Argument passed to \code{\link[base]{gsub}}.
#' @return [\code{atomic}].
#' @export
#' @examples
#' # replace integers
#' x = 1:5
#' mapValues(x, c(2, 3), c(99, 100))
#'
#' # replace factor levels using regex matching
#' x = factor(c("aab", "aba", "baa"))
#' mapValues(x, "a.a", "zzz", regex = TRUE)
mapValues = function(x, from, to, regex = FALSE, ignore.case = FALSE, perl = FALSE, fixed = FALSE) {
  assertAtomic(x)
  assertAtomic(from)
  assertAtomic(to, len = length(from))
  assertFlag(regex)

  map = function(x, from, to) {
    if (regex) {
      for (i in seq_along(from))
        x = gsub(from[i], to[i], x, ignore.case = ignore.case, perl = perl, fixed = fixed)
    } else {
      m = match(x, from, nomatch = NA_integer_)
      found = !is.na(m)
      x[found] = to[m[found]]
    }
    return(x)
  }

  if (is.factor(x)) {
    levels(x) = map(levels(x), from, to)
    return(x)
  }
  return(map(x, from, to))
}