Learn R Programming

fmtr (version 1.0.1)

condition: Define a condition for a user-defined format

Description

The condition function creates a condition for a user-defined format.

Usage

condition(expr, label)

Arguments

expr

A valid R expression. The value in the expression is identified by the variable 'x', i.e. x == 'A' or x > 3 & x < 6. The expression should not be quoted.

label

A label to be assigned if the expression is TRUE. The label can any valid literal value. Typically, the label will be a character string. However, the label parameter does not restrict the data type. Meaning, the label could also be a number, date, or other R object type.

Value

The new condition object.

Details

The condition function creates a condition as part of a format definition. The format is defined using the value function. The condition is defined as an expression/label pair. The expression parameter can be any valid R expression. The label parameter can be any valid literal. Conditions are evaluated in the order they are assigned. A default condition is created by assigning the expression parameter to TRUE. If your data can contain missing values, it is recommended that you test for those values first.

The condition object is an S3 class of type "fmt_cond". The condition labels can be extracted from the format using the labels function.

The format object may be applied to a vector using the fapply function. See link{fapply} for further details.

See Also

fdata to apply formatting to a data frame, value to define a format, levels or labels.fmt to access the labels, and fapply to apply the format to a vector.

Examples

Run this code
# NOT RUN {
# Set up vector
v1 <- c("A", "B", "C", "B")

# Define format
fmt1 <- value(condition(x == "A", "Label A"),
              condition(x == "B", "Label B"), 
              condition(TRUE, "Other"))
              
# Apply format to vector
v2 <- fapply(v1, fmt1)
v2
# }

Run the code above in your browser using DataLab