# NOT RUN {
## Example 1: Simple Formats ##
# Set up data frame
df <- mtcars[1:10, c("mpg", "cyl")]
df
# Define and assign formats
formats(df) <- list( mpg = value(condition(x >= 20, "High"),
condition(x < 20, "Low")),
cyl = value(condition(x == 4, "Small"),
condition(x == 6, "Midsize"),
condition(x == 8, "Large")))
# Apply formatting
fdata(df)
## Example 2: Formatting List ##
# Set up data
v1 <- c("num", "char", "date", "char", "date", "num")
v2 <- list(1.258, "H", Sys.Date(),
"L", Sys.Date() + 60, 2.8865)
df <- data.frame(type = v1, values = I(v2))
df
# Create formatting list
lst <- flist(type = "row", lookup = v1,
num = "%.1f",
char = value(condition(x == "H", "High"),
condition(x == "L", "Low"),
condition(TRUE, "NA")),
date = "%d%b%y")
# Assign list and lookup to column attributes
df$values <- fattr(df$values, format = lst)
# Apply formatting list
fdata(df)
# }
Run the code above in your browser using DataLab