## 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)
# [1] "Label A" "Label B" "Other" "Label B"
## 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)
# [1] 1 2 3 2
## 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)
# [1] "Low" "High" "Out of range" "High"
### Example 4: Conditional formatting
v3 <- c(10.398873, 12.98762, 0.5654, 11.588372)
fmt4 <- value(condition(x < 1, "< 1.0"),
condition(TRUE, "%.2f"))
fapply(v3, fmt4)
# [1] "10.40" "12.99" "< 1.0" "11.59"
Run the code above in your browser using DataLab