Learn R Programming

Kmisc (version 0.2.0)

kReplace: Replace Elements in a Vector

Description

This function replaces elements in a vector. See examples for usage.

Usage

kReplace(vec, from, to = names(from))

Arguments

vec
the vector of items you will be translating
from
the items you will be mapping 'from'
to
the items you will be mapping 'to'. must be same length and order as from.

Examples

Run this code
x <- c(1, 2, 2, 3)
from <- c(1, 2)
to <- c(10, 20)
kReplace( x, from, to )

## alternatively, we can submit a named character vector
## we translate from value to name
names(from) <- to
kReplace( x, from )

## NAs are handled sensibly. Types are coerced as needed.
x <- c(1, NA, 2, 2, 3)
kReplace(x, c(1, 2), c("a", "b") )

Run the code above in your browser using DataLab