paste_mapping: A mapping that adds a prefix and/or suffix

View source: R/mapping.R

paste_mappingR Documentation

A mapping that adds a prefix and/or suffix

Description

A mapping that adds a prefix and/or suffix

Usage

paste_mapping(prefix = NULL, suffix = NULL)

Arguments

prefix, suffix

Character strings.

Value

A mapping function.

Examples


# 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)

benjaminrich/mappings documentation built on June 23, 2024, 1:23 a.m.