tran: Translate elements of a vector

Usage Arguments Examples

View source: R/fun.R

Usage

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

Arguments

from
to
x
tofactor

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (from, to, x, tofactor = is.factor(x)) 
{
    if (is.factor(from)) 
        from <- as.character(from)
    if (is.factor(to)) 
        to <- as.character(to)
    to <- rep(to, length = length(from))
    ret <- x
    if (is.factor(x)) {
        ret <- as.character(ret)
        levs <- levels(x)
    }
    ret <- c(to, unique(ret))[match(ret, c(from, unique(ret)))]
    if (tofactor) {
        if (is.factor(x)) 
            tolevs <- tran(from, to, levs)
        else tolevs <- to
        tolevs <- c(tolevs, unique(ret))
        tolevs <- intersect(tolevs, unique(ret))
        ret <- factor(ret, levels = tolevs)
    }
    ret
  }

gmonette/spida15 documentation built on May 14, 2017, 1:02 p.m.