One advantage of using
fapply() is that your original data is not altered. The formatted values are assigned to a new object. If your original data changes, the formatting function should be reapplied to maintain consistency with the original data.
Data can be formatted with several different types of objects:
You can use the type of formatting object that is most suitable to your data and situation. Each type of formatting object has it’s own strengths and weaknesses.
The formatting functions accept formatting strings such as those associated with the Base R
sprintf() functions. If the data type of the vector is a date or datetime,
fapply() will use the format codes associated with the
format() function. For other data types,
fapply() will use the format codes associated with
sprintf. Here is an example:
<- c(1.367, 8.356, 4.583, 2.873) v1 fapply(v1, "%.1f%%") 1] "1.4%" "8.4%" "4.6%" "2.9%"[
Data may be formatted using a named vector as a lookup. Simply ensure that the names on the formatting vector correspond to the values in the data vector.
The advantage of using a named vector for formatting is its simplicity. The disadvantage is that it only works with character values. Here is an example of formatting using a named vector:
The fmtr package provides custom functions for creating user-defined formats, in a manner that is similar to a SAS® user-defined format. These functions are
value() function accepts one or more conditions. The
condition() function accepts an expression/label pair. A user-defined format has the advantage of a clear and flexible syntax. It is excellent for categorizing data. Here is an example of a user-defined format:
Vectorized functions provide the most powerful way of formatting data. Vectorized functions can be user-defined, or wrapping an available packaged function. The vectorized function has the advantage of being nearly limitless in the types of formatting you can perform. The drawback is that a vectorized function can be more complicated to write. Here is an example of formatting with a user-defined, vectorized function:
Sometimes data needs to be formatted differently for each row. This situation is difficult to deal with in any language.
But it can be made easy in R with the fmtr package and a formatting list.
A formatting list is a list that contains one or more of the four types of formatting objects described above. It is defined with the
flist() function. A formatting list can be applied in two different ways: in order, or with a lookup.
By default, the list is applied in order. That means the first format in the list is applied to the first item in the vector, the second format in the list is applied to the second item in the vector, and so on. The list is recycled if the number of list items is shorter than the number of values in the vector.
For the lookup method, the formatting object is specified by a lookup vector. The lookup vector should contain names associated with the elements in the formatting list. The lookup vector should also contain the same number of items as the data vector. For each item in the data vector, fmtr will look up the appropriate format from the formatting list, and apply that format to the corresponding data value.
The following is an example of a lookup style formatting list:
# Set up data v1 <- c("num", "char", "date", "char", "date", "num") v2 <- list(1.258, "H", as.Date("2020-06-19"), "L", as.Date("2020-04-24"), 2.8865) df <- data.frame(type = v1, values = I(v2)) df # type values # 1 num 1.258 # 2 char H # 3 date 2020-06-19 # 4 char L # 5 date 2020-04-24 # 6 num 2.8865 # Set up formatting list lst <- flist(type = "row", lookup = v1, num = "%.1f", char = value(condition(x == "H", "High"), condition(x == "L", "Low"), condition(TRUE, "NA")), date = "%y-%m") # Assign formatting list to values column attr(df$values, "format") <- lst # Apply formatting fdata(df) # type values # 1 num 1.3 # 2 char High # 3 date 20-06 # 4 char Low # 5 date 20-04 # 6 num 2.9
Next: Format Catalogs