decipher: replace selected values

decipherR Documentation

replace selected values

Description

Change values that corresponds to those of a key.

Usage

decipher(x, key, flexible = TRUE, within = FALSE)

translate(x, key, flexible = TRUE, within = FALSE)

Arguments

x

Character (or factor) vector

key

Named character vector; where (typically) the names corresponds to values appearing in x, and the values are those we want as replacements. However, if flexible = TRUE, then a check is performed to see if it makes more sense to invert the key.

flexible

Logical; if TRUE the key can be inverted

within

Logical; if TRUE occurences of the keyed values within a string can be replaced. Such occurences must be encapsulated by beginning of line, space characters, punctuation characters or end of line, so that replacement does not take place within words.

Value

vector of same length and class as s (character or factor)

Examples

x <- c("foo", "foo!", "A foo", "A foo and two bar",
       "football", "barely", "bar")
key <- c(foo = "yay", bar = "bohnanza")
data.frame(
  x = x,
  d0 = key[x], ## useful only if all values of s exists in the key
  d1 = decipher(x, key),
  d2 = decipher(x, key, within = TRUE)
)
x <- factor(c(1,3,2), levels = 1:3,
            labels = c("foo", "bar", "baz"))
key <- c(A = "foo", C = "baz")
str(data.frame(x = x, d = decipher(x, key)))

renlund/descripteur documentation built on Sept. 24, 2023, 7:07 p.m.