Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

mapvalues

Replace specified values with new values, in a vector or factor.


Description

Item in x that match items from will be replaced by items in to, matched by position. For example, items in x that match the first element in from will be replaced by the first element of to.

Usage

mapvalues(x, from, to, warn_missing = TRUE)

Arguments

x

the factor or vector to modify

from

a vector of the items to replace

to

a vector of replacement values

warn_missing

print a message if any of the old values are not actually present in x

Details

If x is a factor, the matching levels of the factor will be replaced with the new values.

The related revalue function works only on character vectors and factors, but this function works on vectors of any type and factors.

See Also

revalue to do the same thing but with a single named vector instead of two separate vectors.

Examples

x <- c("a", "b", "c")
mapvalues(x, c("a", "c"), c("A", "C"))

# Works on factors
y <- factor(c("a", "b", "c", "a"))
mapvalues(y, c("a", "c"), c("A", "C"))

# Works on numeric vectors
z <- c(1, 4, 5, 9)
mapvalues(z, from = c(1, 5, 9), to = c(10, 50, 90))

plyr

Tools for Splitting, Applying and Combining Data

v1.8.6
MIT + file LICENSE
Authors
Hadley Wickham [aut, cre]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.