knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
Data can be formatted by assigning formats to the format attribute
of the columns in your dataframe or tibble, and then by calling the fdata()
function on that data. A sample program is as follows:
# Set up data frame df <- mtcars[1:10, c("mpg", "cyl")] df # Define and assign formats attr(df$mpg, "format") <- value(condition(x >= 20, "High"), condition(x < 20, "Low")) attr(df$cyl, "format") <- function(x) format(x, nsmall = 1) # Apply formatting fdata(df)
Here is the mtcars subset before formatting:
# mpg cyl # Mazda RX4 21.0 6 # Mazda RX4 Wag 21.0 6 # Datsun 710 22.8 4 # Hornet 4 Drive 21.4 6 # Hornet Sportabout 18.7 8 # Valiant 18.1 6 # Duster 360 14.3 8 # Merc 240D 24.4 4 # Merc 230 22.8 4 # Merc 280 19.2 6
And here is the mtcars subset after formatting:
# mpg cyl # Mazda RX4 High 6.0 # Mazda RX4 Wag High 6.0 # Datsun 710 High 4.0 # Hornet 4 Drive High 6.0 # Hornet Sportabout Low 8.0 # Valiant Low 6.0 # Duster 360 Low 8.0 # Merc 240D High 4.0 # Merc 230 High 4.0 # Merc 280 Low 6.0
You may apply formatting to variables of any data type: character, numeric,
date, etc. Internally, the fdata()
function is using the fapply()
function on each column in the data frame. If there is no format assigned
to a column, that column is returned unaltered.
Next: Format Apply Function
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.