mapping: Generate a Mapping Function

Description Usage Arguments Details Value Author(s) Examples

Description

This function returns a function that does a simple mapping from one set of value to another. It is a function-generating function.

Usage

1
mapping(from, to, ch.as.fact = TRUE)

Arguments

from

A vector. This is the domain of the function.

to

A vector of the same length as from.

ch.as.fact

A logical. Should the mapping return a factor instead of character?

Details

This function returns a function. When called with a vector argument x, this function will return a vector y of the same length as x and such that each element y[i] is equal to to[j] where j is the smallest integer such that from[j] == x[i], and NA if no such j exists.

Value

A function that translates from from to to. The function also has a attribute "inverse" which is a function that performs the inverse mapping.

Author(s)

Ben Rich <benjamin.rich@certara.com>

Examples

1
2
3
4
5
6
7
8
sex.mapping <- mapping(c("Female", "F", "Male", "M"), c(0, 0, 1, 1))
sex.mapping(c("Female", "Female", "Male", "F"))

sex.inverse.mapping <- attr(sex.mapping, "inverse")
sex.inverse.mapping(c(0, 0, 1, 0))

race.mapping <- mapping(c(1, 2, 5), c("WHITE", "BLACK OR AFRICAN AMERICAN", "AMERICAN INDIAN OR ALASKA NATIVE"))
race.mapping(1:5)

benjaminrich/PCSmisc documentation built on May 24, 2019, 5:02 a.m.