fmap: Create a Key-Value Mapping for Format Creation

View source: R/utilities.R

fmapR Documentation

Create a Key-Value Mapping for Format Creation

Description

Convenience helper for building data-driven formats with fnew. Returns a named vector (or list) with class "ks_fmap" that signals fnew() to use the natural direction: names are input keys, values are output labels/objects — regardless of the format type.

Usage

fmap(keys, values)

Arguments

keys

Character vector of input keys (lookup values).

values

Vector of output labels or objects (character, numeric, Date, POSIXct, logical, etc.).

Details

Without fmap(), fnew() reverses named vectors for character and numeric types (the factor() convention c(Label = "Code")). Wrapping your data in fmap() suppresses this reversal, so fmap(keys, values) works identically for character, numeric, Date, POSIXct, and logical formats.

Value

A named vector (or list, for non-atomic values) with class c("ks_fmap", <original class>). Names are keys, values are values.

See Also

fnew for format creation.

Examples

# Character lookup: keys -> labels
fmap(c("M", "F"), c("Male", "Female")) |> fnew(name = "sex")
fput(c("M", "F"), "sex")
fclear()

# Date lookup from a data frame
ids   <- c("SUBJ-001", "SUBJ-002")
dates <- as.Date(c("2023-03-09", "2024-08-13"))
fmap(ids, dates) |> fnew(type = "Date", name = "icdtn")
fput("SUBJ-001", "icdtn")
fclear()

ksformat documentation built on May 21, 2026, 9:07 a.m.