Title: | Functions for Transforming Categorical Variables |
---|---|
Description: | Easily create functions to map between different sets of values, such as for re-labeling categorical variables. |
Authors: | Benjamin Rich [aut, cre, cph] |
Maintainer: | Benjamin Rich <[email protected]> |
License: | GPL-3 |
Version: | 1.0.0 |
Built: | 2024-11-16 05:04:55 UTC |
Source: | https://github.com/benjaminrich/mappings |
data.frame
The resulting data.frame
has 2 columns: mapsfrom
, and mapsto
.
## S3 method for class 'mapping' as.data.frame(x, ...)
## S3 method for class 'mapping' as.data.frame(x, ...)
x |
A |
... |
Ignored. |
A data.frame
.
factor
from one or more vectorsA factor
is constructed from one or more atomic vectors. If more than
one atomic vector is supplied, then a compound value is constructed by
concatenating the values together. The order of the levels is the natural
order in which the values appear.
cf(x, ..., sep = ";")
cf(x, ..., sep = ";")
x |
An atomic vector. |
... |
Additional atomic vectors (optional). |
sep |
A |
A factor
.
x <- c("A", "B", "A") y <- c(2, 5, 7) cf(x, y) mapping(cf(x, y), c("X", "Y", "Z"))
x <- c("A", "B", "A") y <- c(2, 5, 7) cf(x, y) mapping(cf(x, y), c("X", "Y", "Z"))
Mapping from continuous to categorical
cut_mapping(..., to = NULL, na = NA, ch.as.fact = TRUE)
cut_mapping(..., to = NULL, na = NA, ch.as.fact = TRUE)
... |
Passed to |
to |
Passed to |
na |
Passed to |
ch.as.fact |
Passed to |
A function that cuts a numeric
vector and maps the result.
x <- c(0, 10, 20, 30, Inf) m <- cut_mapping(x, right=FALSE, to=c("0 to <10", "10 to <20", "20 to <30", ">= 30")) print(m) m(c(5, 27, 3, 10, 99))
x <- c(0, 10, 20, 30, Inf) m <- cut_mapping(x, right=FALSE, to=c("0 to <10", "10 to <20", "20 to <30", ">= 30")) print(m) m(c(5, 27, 3, 10, 99))
Domain and codomain of a mapping.
domain(x) codomain(x)
domain(x) codomain(x)
x |
A |
x A vector of the same type as we supplied when the
mapping
was created.
These aren't the true domain and codomain in the mathematical sense; both can contain duplicates.
sex.mapping <- mapping(c("Female", "F", "Male", "M"), c(0, 0, 1, 1)) domain(sex.mapping) codomain(sex.mapping)
sex.mapping <- mapping(c("Female", "F", "Male", "M"), c(0, 0, 1, 1)) domain(sex.mapping) codomain(sex.mapping)
Given a mapping
x
, return the inverse mapping.
inverse(x)
inverse(x)
x |
A |
The inverse mapping
.
sex.mapping <- mapping(c("Female", "F", "Male", "M"), c(0, 0, 1, 1)) sex.inverse.mapping <- inverse(sex.mapping) sex.inverse.mapping(c(0, 0, 1, 0))
sex.mapping <- mapping(c("Female", "F", "Male", "M"), c(0, 0, 1, 1)) sex.inverse.mapping <- inverse(sex.mapping) sex.inverse.mapping(c(0, 0, 1, 0))
This function returns a function that does a simple mapping from one set of value to another. It is a function-generating function.
mapping(from, to, na = NA, ch.as.fact = TRUE, unmapped = NA) pmapping(..., unmapped = I)
mapping(from, to, na = NA, ch.as.fact = TRUE, unmapped = NA) pmapping(..., unmapped = I)
from |
A vector. This is the domain of the function. |
to |
A vector of the same length as |
na |
An alternative way to specify the value that |
ch.as.fact |
A logical. Should the mapping return a |
unmapped |
This is a fallback for the case when a value can't be mapped
because it doesn't match any of the elements in |
... |
Passed to |
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 the value unmapped
(or, if it's a function,
unmapped(x[i])
) if no such j
exists.
pmapping()
creates a partial mapping, which maps certain elements while
preserving the rest (by making unmapped=I
the default).
Note: from
will always be matched as a string, even if it is numeric.
So, mapping(1, "A")
and mapping("1", "A")
are the same, and
both functions will return "A"
when called with either 1
or
"1"
.
A function that translates from from
to to
. The function also
has an inverse
which is a function that performs the inverse mapping.
inverse()
,
codomain()
,
domain()
,
remap()
,
text2mapping()
,
cut_mapping()
sex.mapping <- mapping(c("Female", "F", "Male", "M"), c(0, 0, 1, 1)) sex.mapping(c("Female", "Female", "Male", "F")) sex.mapping <- mapping(0:1, c("Female", "Male"), na="Unknown") sex.mapping(c(0, 1, NA, 0, 1, 1, 0)) inverse(sex.mapping)(c("Female", "Male", "Unknown")) from <- c(0, 1, NA) to <- c(NA, "Male", "Female") x <- c(0, 1, NA, 0, 1, 1, 0) sex.mapping <- mapping(c(0, 1, NA), c(NA, "Male", "Female")) sex.mapping sex.mapping(c(0, 1, NA, 0, 1, 1, 0)) inverse(sex.mapping) inverse(sex.mapping)(c("Female", "Male", NA)) race.mapping <- mapping(c( "1"="WHITE", "2"="BLACK OR AFRICAN AMERICAN", "5"="AMERICAN INDIAN OR ALASKA NATIVE")) race.mapping(1:5) # Use of `unmapped` dv.mapping <- mapping("BQL", -99, unmapped=as.numeric) dv.mapping(c("3.1", "BQL", "2.7", "100")) # Map certain elements and preserves the rest x <- LETTERS[1:5] pmapping("B", "Z")(x) mapping("B", "Z", unmapped=I)(x) # Same
sex.mapping <- mapping(c("Female", "F", "Male", "M"), c(0, 0, 1, 1)) sex.mapping(c("Female", "Female", "Male", "F")) sex.mapping <- mapping(0:1, c("Female", "Male"), na="Unknown") sex.mapping(c(0, 1, NA, 0, 1, 1, 0)) inverse(sex.mapping)(c("Female", "Male", "Unknown")) from <- c(0, 1, NA) to <- c(NA, "Male", "Female") x <- c(0, 1, NA, 0, 1, 1, 0) sex.mapping <- mapping(c(0, 1, NA), c(NA, "Male", "Female")) sex.mapping sex.mapping(c(0, 1, NA, 0, 1, 1, 0)) inverse(sex.mapping) inverse(sex.mapping)(c("Female", "Male", NA)) race.mapping <- mapping(c( "1"="WHITE", "2"="BLACK OR AFRICAN AMERICAN", "5"="AMERICAN INDIAN OR ALASKA NATIVE")) race.mapping(1:5) # Use of `unmapped` dv.mapping <- mapping("BQL", -99, unmapped=as.numeric) dv.mapping(c("3.1", "BQL", "2.7", "100")) # Map certain elements and preserves the rest x <- LETTERS[1:5] pmapping("B", "Z")(x) mapping("B", "Z", unmapped=I)(x) # Same
A mapping that adds a prefix and/or suffix
paste_mapping(prefix = NULL, suffix = NULL)
paste_mapping(prefix = NULL, suffix = NULL)
prefix , suffix
|
Character strings. |
A mapping
function.
# The objective is to turn a numeric vector into a factor such that # the levels preserve the numeric order but contain the suffix "mg" # (i.e., so that 2 becomes "2 mg" for instance) x <- c(1, 2, 1, 10, 3, 2, 2, 1) # The following does not produce the levels in the desired numeric order # (because alphabetical ordering places "10" before "2") factor(paste(x, "mg")) # The following works, but takes 2 lines of code and requires a variable # assignment y <- factor(x) levels(y) <- paste(levels(y), "mg") y # This does the same thing with one line of code and no assignment paste_mapping(, " mg")(x) # ----- # In this example, you start with a factor, and want to preserve its ordering x <- factor(c("Treatment", "Placebo"), levels=c("Treatment", "Placebo")) # Again, this won't work as desired factor(paste("Randomized to", x, "Group")) # But this will paste_mapping("Randomized to ", " Group")(x)
# The objective is to turn a numeric vector into a factor such that # the levels preserve the numeric order but contain the suffix "mg" # (i.e., so that 2 becomes "2 mg" for instance) x <- c(1, 2, 1, 10, 3, 2, 2, 1) # The following does not produce the levels in the desired numeric order # (because alphabetical ordering places "10" before "2") factor(paste(x, "mg")) # The following works, but takes 2 lines of code and requires a variable # assignment y <- factor(x) levels(y) <- paste(levels(y), "mg") y # This does the same thing with one line of code and no assignment paste_mapping(, " mg")(x) # ----- # In this example, you start with a factor, and want to preserve its ordering x <- factor(c("Treatment", "Placebo"), levels=c("Treatment", "Placebo")) # Again, this won't work as desired factor(paste("Randomized to", x, "Group")) # But this will paste_mapping("Randomized to ", " Group")(x)
Print a mapping
## S3 method for class 'mapping' print(x, ...)
## S3 method for class 'mapping' print(x, ...)
x |
|
... |
Ignored. |
Returns x
invisibly.
Apply a mapping to a vector directly. The mapping is temporary and not saved.
remap(x, ...)
remap(x, ...)
x |
The values to apply the |
... |
Passed to |
The values returned by calling the mapping
function.
x <- c("A", "B", "A") remap(x, c(A=0, B=1))
x <- c("A", "B", "A") remap(x, c(A=0, B=1))
Convenient shorthand for specifying mappings with text strings
text2mapping( text, file = NULL, sep = "|", flip = FALSE, convert.na = TRUE, numericWherePossible = TRUE, ... )
text2mapping( text, file = NULL, sep = "|", flip = FALSE, convert.na = TRUE, numericWherePossible = TRUE, ... )
text |
A multi-line string specifying a mapping with 2 columns (see examples). |
file |
If |
sep |
Character used as column separator. |
flip |
If |
convert.na |
If |
numericWherePossible |
If |
... |
Further arguments passed to |
A mapping
.
f <- text2mapping(" L | Low M | Medium H | High ") f(warpbreaks$tension)
f <- text2mapping(" L | Low M | Medium H | High ") f(warpbreaks$tension)