View source: R/format_create.R
| fnew | R Documentation |
Creates a format object that maps values to labels, similar to 'SAS' PROC FORMAT.
Supports discrete value mapping, ranges, and special handling of missing values.
The format is automatically stored in the global format library if name
is provided.
fnew(
...,
name = NULL,
type = "auto",
default = NULL,
multilabel = FALSE,
ignore_case = FALSE,
date_format = NULL,
range_subtype = c("numeric", "date", "datetime"),
strata_sep = "|",
verbose = FALSE
)
... |
Named arguments defining value-label mappings, or one or more
named vectors/lists using the R convention
Named vectors use the R idiom where names are labels and values are codes,
which is the reverse of the Named-vector reversal: For character and numeric formats, named
vectors are automatically reversed so that Data-driven formats: For formats built programmatically from
data, wrap your data in |
name |
Character. Optional name for the format. If provided, the format is automatically registered in the global format library. |
type |
Character. Type of format: |
default |
Character. Default label for unmatched values (overrides .other) |
multilabel |
Logical. If |
ignore_case |
Logical. If |
date_format |
Character. Optional strptime-style format string used
when parsing date/datetime range keys (e.g. |
range_subtype |
Character. For |
strata_sep |
Character. For |
verbose |
Logical. If |
Special directives:
.missing: Label for NA, NULL, NaN values
.other: Label for values not matching any rule
Named-vector direction (reverse convention):
When a named vector or list is passed as an unnamed argument (e.g.,
fnew(c(Male = "M"))), the direction of the name-to-value mapping
depends on the output type:
For character / numeric types, names are labels and
values are codes. The pairs are reversed internally so that the
format maps code -> label. This follows the standard R idiom used
by factor(), where c(Label = "Code").
For value types (Date, POSIXct,
logical), names are input keys and values are the native R
objects returned by the format. No reversal is applied, because
non-character objects cannot be used as vector names.
This means the same data may need to be arranged differently
depending on the target type. To avoid this inconsistency for data-driven
formats, use fmap(keys, values) which works identically
for all types:
fnew(fmap(ids, dates), type = "Date") fnew(fmap(ids, date_strings), type = "character")
When in doubt, use explicit key = "label" arguments — these are
never reversed regardless of type.
Expression labels: If a label contains .x1, .x2, etc.,
it is treated as an R expression that is evaluated at apply-time. Extra arguments
are passed positionally via ... in fput:
stat_fmt <- fnew("n" = "sprintf('%s', .x1)",
"pct" = "sprintf('%.1f%%', .x1 * 100)")
fput(c("n", "pct"), stat_fmt, c(42, 0.15))
# Returns: "42" "15.0%"
An object of class "ks_format" containing the format definition.
The object is also stored in the format library if name is given.
# Discrete value format (auto-stored as "sex")
fnew(
"M" = "Male",
"F" = "Female",
.missing = "Unknown",
.other = "Other Gender",
name = "sex"
)
# Apply immediately
fput(c("M", "F", NA, "X"), "sex")
# [1] "Male" "Female" "Unknown" "Other Gender"
fclear()
# Multilabel format: a value can match multiple labels
fnew(
"0,5,TRUE,TRUE" = "Infant",
"6,11,TRUE,TRUE" = "Child",
"12,17,TRUE,TRUE" = "Adolescent",
"0,17,TRUE,TRUE" = "Pediatric",
"18,64,TRUE,TRUE" = "Adult",
"65,Inf,TRUE,TRUE" = "Elderly",
"18,Inf,TRUE,TRUE" = "Non-Pediatric",
name = "age_categories",
type = "numeric",
multilabel = TRUE
)
# fput returns first match; fput_all returns all matches
fput(c(3, 14, 25, 70), "age_categories")
fput_all(c(3, 14, 25, 70), "age_categories")
fclear()
# From a named vector (Label = Code convention)
sex_vec <- fnew(c(Male = "M", Female = "F"), .missing = "Unknown",
name = "sex_vec")
fput(c("M", "F", NA), sex_vec)
# [1] "Male" "Female" "Unknown"
fclear()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.