as.fcat.data.frame: Convert a data frame to a format catalog

Description Usage Arguments Details Value Input Data Frame Specifications See Also Examples

View source: R/fcat.R

Description

This function takes a data frame as input and converts it to a format catalog based on the information contained in the data frame. The data frame should have 5 columns: "Name", "Type", "Expression", "Label" and "Order".

Usage

1
2
## S3 method for class 'data.frame'
as.fcat(x)

Arguments

x

The data frame to convert.

Details

The as.fcat.data.frame converts a data frame to a format catalog. A corresponding conversion for class "tbl_df" converts a tibble.

To understand the format of the input data frame, create a format and use the as.data.frame method to convert the format to a data frame. Then observe the columns and organization of the data.

Value

A format catalog based on the information contained in the input data frame.

Input Data Frame Specifications

The input data frame should contain the following columns:

Any additional columns will be ignored. Column names are case-insensitive.

Valid values for the "Type" column are as follows:

The "Label" and "Order" columns are used only for a type "U", user-defined format created with the value function.

See Also

Other fcat: as.data.frame.fcat(), as.fcat.fmt_lst(), as.fcat.list(), as.fcat(), fcat(), is.fcat(), print.fcat(), read.fcat(), write.fcat()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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)

# Use re-converted catalog
fapply(123.456, c2$num_fmt)
fapply(c("A", "B", "C", "B"), c2$label_fmt)
fapply(Sys.Date(), c2$date_fmt)

fmtr documentation built on July 25, 2021, 9:06 a.m.