Learn R Programming

fmtr (version 1.0.1)

value: Create a user-defined format

Description

The value function creates a user-defined format.

Usage

value(...)

Arguments

...

One or more conditions.

Value

The new format object.

Details

The value function creates a user defined format object, in a manner similar to a SAS<U+00AE> format. The value function accepts one or more condition arguments that define the format. The conditions map an R expression to a label. When applied, the format will return the label corresponding to the first true expression.

The format object is an S3 class of type "fmt". When the object is created, the levels attribute of the object will be set with a vector of values assigned to the labels property of the condition arguments. These labels may be accessed either from the levels function or the labels function.

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

See Also

condition to define a condition, levels or labels to access the labels, and fapply to apply the format to a vector.

Examples

Run this code
# NOT RUN {
## Example 1: Character to Character Mapping ##
# 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
fapply(v1, fmt1)


## Example 2: Character to Integer Mapping ##
fmt2 <- value(condition(x == "A", 1),
              condition(x == "B", 2),
              condition(TRUE, 3))

# Apply format to vector
fapply(v1, fmt2)


## Example 3: Categorization of Continuous Variable ##
# Set up vector
v2 <- c(1, 6, 11, 7)

# Define format
fmt3 <- value(condition(x < 5, "Low"),
              condition(x >= 5 & x < 10, "High"), 
              condition(TRUE, "Out of range"))
              
# Apply format to vector
fapply(v2, fmt3)
# }

Run the code above in your browser using DataLab