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

recode2

bivariate recode


Description

Recode x1 and x2 per the lexical codes table.

Usage

recode2(x1, x2, codes)

Arguments

x1, x2

vectors of the same length assuming a discrete number of levels

codes

a 2-dimensional matrix indexed by the levels of x1 and x2. If dimnames(codes) are not provided, they are assumed to unique(x1) (or unique(x2)).

Details

1. If length(x1) != length(x2), complain.

2. if(is.logical(x1)) l1 <- c(FALSE, TRUE) else l1 <- unique(x1); ditto for x2.

3. If(missing(codes)) codes <- outer(unique(x1), unique(x2))

4. if(is.null(dim(codes))) dim(codes) <- c(length(unique(x1)), length(unique(x2)))

5. If is.null(rownames(codes)), set as follows: If nrow(codes) == length(unique(x1)), rownames(codes) <- unique(x1). Else, if nrow(codes) = max(x1), set rownames(codes) <- seq(1, max(x1)). Else throw an error. Ditto for colnames, ncol, and x2.

6. codes[x1, x2]

Value

a vector of the same length as x1 and x2.

Author(s)

Spencer Graves

See Also

dim rownames link{colnames}

Examples

contrib <- c(-1, 0, 0, 1)
contrib0 <- c(FALSE, FALSE, TRUE, FALSE)

contribCodes <- recode2(contrib>0, contrib0,
   c('returned', 'received', '0', 'ERR') )

cC <- c('returned', 'returned', '0', 'received')

all.equal(contribCodes, cC)

Ecfun

Functions for 'Ecdat'

v0.2-4
GPL (>= 2)
Authors
Spencer Graves <spencer.graves@effectivedefense.org>
Initial release
2020-10-26

We don't support your browser anymore

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