tran: Translate elements of a vector

View source: R/tran.R

tranR Documentation

Translate elements of a vector

Description

This should return something like 'to' whenever all of 'x' is in 'from' Otherwise, if some of x is not modified then it needs to either to change those to NAs or to leave them as is. For a strict function, we can simply index to which will leave it unchanged which conflicts with my versions of tr, ergo this is called tran

tr changes the order of arguments in tran.

Usage

tran(from, to, x, tofactor = is.factor(x))

tr(x, from, to)

Arguments

from

numerical, logical or character vector of values or factor levels to change

to

vector of corresponding values to change into

x

numeric, logical, character vector or factor to modify

tofactor

if TRUE, value will be a factor, default: is.factor(x)

Examples

tran(FALSE, TRUE, c(FALSE, TRUE, NA))
tran(c(FALSE, NA), c(TRUE, FALSE), c(FALSE, TRUE, NA))
tran(c(FALSE, NA), c(TRUE, FALSE), c(FALSE, TRUE, NA)) %>% mode
# numeric
tran(c(1,2), c(11,12), 1:5)
tran(NA, 99, c(1:5, NA))
tran(NA, 99, c(1:5, NA)) %>% mode
# mixed
tran(c(1,2,10), c('a','b', 'z'), 1:5)
# factor
tran(letters, 1:26, factor(c('a','b','z')) )
tran('a', 1, factor(c('a','b','z')) )

gmonette/spida2 documentation built on Aug. 20, 2023, 7:21 p.m.