Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

as.fcat.tbl_df

Convert a data frame to a format catalog


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

## S3 method for class 'tbl_df'
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:

  • Name: The name of the format

  • Type: The type of format. See the type codes below.

  • Expression: The formatting expression. The expression will hold different types of values depending on the format type.

  • Label: The label for user-defined, "U" type formats.

  • Order: The order for user-defined, "U" type formats.

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

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

  • U: User Defined List created with the value function.

  • S: A formatting string of formatting codes. See FormattingStrings.

  • F: A vectorized function.

  • V: A named vector lookup.

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

See Also

Examples

# 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

Easily Apply Formats to Data

v1.5.0
CC0
Authors
David Bosak
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.