Replace: Replace elements of a vector

Description Usage Arguments Value Examples

View source: R/utilities.R

Description

Replace replaces the values in x which are equal to from to the values given in to. The values in to are recycled to match the length of from.

Usage

1
Replace(x, from, to, digits = 10L)

Arguments

x

a raw, integer, character or numeric (double) vector, or a factor

from

vector of elements to replace

to

the elements

digits

integer indicating the number of decimal places to be used in the comparison of double values (ignored if is.double(x) is FALSE)

Value

a vector with replaced values

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
x <- c(NA, c("a", "a", "z", "e", "q"))

# note that 'from' might contain values which are not present in 'x'
xr <- Replace(x, from = c("w", "a", "e"), to = c("ww", "aa", "ee"))

# only 'a' and 'e' were replaced
xr
stopifnot(identical(length(x), length(xr)))
stopifnot(identical(xr[c(2, 3, 5)], c("aa", "aa", "ee")))

# the missing value is not affected
stopifnot(is.na(xr[1]))

tdeenes/eegR documentation built on April 19, 2021, 4:17 p.m.