| fput | R Documentation |
Applies a format definition to a vector of values, returning formatted labels. Properly handles NA, NULL, NaN, and other missing values.
fput(x, format, ..., keep_na = FALSE)
x |
Vector of values to format |
format |
A |
... |
Additional arguments for expression labels. Positional arguments
are mapped to |
keep_na |
Logical. If TRUE, preserve NA in output instead of applying missing label. |
The function handles missing values in the following order:
NA, NULL, NaN -> Uses format's missing_label if defined
Exact matches -> Uses defined value-label mapping
Range matches (for numeric) -> Uses range label
No match -> Uses format's other_label or returns original value
Expression labels: If a label string contains .x1, .x2,
etc., it is evaluated as an R expression at apply-time. Extra data is passed
as positional arguments:
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%"
Case-insensitive matching: When a format has ignore_case = TRUE,
key matching is case-insensitive for character formats.
Character vector with formatted labels
# Basic discrete formatting
fnew("M" = "Male", "F" = "Female", .missing = "Unknown", name = "sex")
fput(c("M", "F", NA, "X"), "sex")
# [1] "Male" "Female" "Unknown" "X"
# Preserve NA instead of applying missing label
sex_f <- fnew("M" = "Male", "F" = "Female", .missing = "Unknown")
fput(c("M", "F", NA), sex_f, keep_na = TRUE)
# [1] "Male" "Female" NA
# Numeric range formatting
fparse(text = '
VALUE score (numeric)
(0, 50] = "Low"
(50, 100] = "High"
.other = "Out of range"
;
')
fput(c(0, 1, 50, 51, 100, 101), "score")
# [1] "Out of range" "Low" "Low" "High" "High" "Out of range"
fclear()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.