Learn R Programming

mark (version 0.8.1)

recode_by: Recode by

Description

A simple implementation of recoding

Usage

recode_by(x, by, vals = NULL, mode = "any")

recode_only(x, by, vals = NULL)

Value

A vector of values from x

Arguments

x

A vector to recode

by

A names vector (new = old); any non-matching values are set to the appropriate NA

vals

An optional vector of values to use in lieu of a names in the vector; this takes priority over names(by). This can be the same length as by or a single value.

mode

passed to as.vector()

Details

This can be comparable to dplyr::recode() expect that the values are arranged as new = old rather than old = new and allows for a separate vector to be passed for new.

recode_only() will only recode the values matches in by/val. The mode is automatically set according to mode(x). This functions more like base::replace() but with extra features

See Also

Examples

Run this code
recode_by(1:3, c(a = 1, b = 2))
recode_by(letters[1:3], c(`1` = "a", `2` = "b"))                   # will not guess mode
recode_by(letters[1:3], c(`1` = "a", `2` = "b"), mode = "integer") # make as integer
recode_by(letters[1:3], c("a", "b"), vals = 1:2)                   # or pass to vals

recode_only(letters[1:3], c("zzz" = "a"))
recode_only(letters[1:3], c(`1` = "a")) # returns as "1"
recode_only(1:3, c("a" = 1))            # coerced to NA

# Pass list for multiples
recode_only(letters[1:10], list(abc = c("a", "b", "c"), ef = c("e", "f")))

Run the code above in your browser using DataLab