# Create a format catalog
c1 <- fcat(num_fmt = "%.1f",
label_fmt = value(condition(x == "A", "Label A"),
condition(x == "B", "Label B"),
condition(TRUE, "Other")),
date_fmt = "%d-%b-%Y")
# Convert catalog to data frame to view the structure
df <- as.data.frame(c1)
print(df)
# Name Type Expression Label Order
# 1 num_fmt S %.1f NA
# 2 label_fmt U x == "A" Label A NA
# 3 label_fmt U x == "B" Label B NA
# 4 label_fmt U TRUE Other NA
# 5 date_fmt S %d-%b-%Y NA
# Convert data frame back to a format catalog
c2 <- as.fcat(df)
c2
# # A format catalog: 3 formats
# - $date_fmt: type S, "%d-%b-%Y"
# - $label_fmt: type U, 3 conditions
# - $num_fmt: type S, "%.1f"
# Use re-converted catalog
fapply(123.456, c2$num_fmt)
# [1] "123.5"
fapply(c("A", "B", "C", "B"), c2$label_fmt)
# [1] "Label A" "Label B" "Other" "Label B"
fapply(Sys.Date(), c2$date_fmt)
# [1] "07-Jan-2024"
Run the code above in your browser using DataLab