mapping: Generate a Mapping Function

View source: R/PCSmisc.R

mappingR Documentation

Generate a Mapping Function

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

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)

Benjamin Rich <mail@benjaminrich.net>

Examples


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 Feb. 11, 2024, 9:25 p.m.