Formatting codes for formatting strings follow the conventions
of the base R strptime
and sprintf
functions. See below for further details.
The fmtr packages accepts single strings as formatting
specifications. These formatting strings are interpreted differently
depending on the data type of the vector being formatted. For
date and datetime vectors, the string will be interpreted as an input
to the base R strptime
function. For all other types of vectors,
the formatting string will be interpreted as an input to the sprintf
function.
The formatting codes for these functions are simple to use. For example,
the code fapply(as.Date("1970-01-01"), "%B %d, %Y")
will produce
the output "January 01, 1970"
. The code
fapply(1.2345, "%.1f")
will produce the output "1.2"
.
Below are some commonly used formatting codes for dates:
%d = day as a number
%a = abbreviated weekday
%A = unabbreviated weekday
%m = month
%b = abbreviated month
%B = unabbreviated month
%y = 2-digit year
%Y = 4-digit year
%H = hour (24 hour clock)
%I = hour (12 hour clock)
%M = minute
%S = second
%p = AM/PM indicator
%q = Quarter as integer
%Q = Quarter as "Q?"
See the strptime
function for additional codes and
examples of formatting dates and times.
Below are some commonly used formatting codes for other data types:
%s = string
%d = integer
%f = floating point number
See the sprintf
function for additional codes and
examples of formatting other data types.
The "best" format is a special numeric format that replicates the "best" style of formatting from SAS. The "best" format automatically determines the best way to format a number within a specified with.
For example, if you have a number like 123.45678, and format it with "best6", the result will be "123.46". Note that the decimal point counts as one of the digits. Also note that the rightmost digit will be rounded according to SAS rounding rules.
The same number above formatted with "best8" will be "123.4567". The default width is 12. That means, if you simply send the format "best" with no width, it will be interpreted as "best12".
# Examples for formatting dates and times
t <- Sys.time()
fapply(t, "%d/%m/%Y") # Day/Month/Year
#> [1] "06/10/2025"
fapply(t, "%d%b%Y") # Day abbreviated month year
#> [1] "06Oct2025"
fapply(t, "%y-%m") # Two digit year - month
#> [1] "25-10"
fapply(t, "%A, %B %d") # Weekday, unabbreviated month and date
#> [1] "Monday, October 06"
fapply(t, "%Y-%Q") # Year and Quarter
#> [1] "2025-Q4"
fapply(t, "%Y-%m%-%d %H:%M:%S %p") # Common timestamp format
#> [1] "2025-10-06 00:00:32 AM"
# Examples for formatting numbers
a <- 1234.56789
fapply(a, "%f") # Floating point number
#> [1] "1234.567890"
fapply(a, "%.1f") # One decimal place
#> [1] "1234.6"
fapply(a, "%8.1f") # Fixed width
#> [1] " 1234.6"
fapply(a, "%-8.1f") # Fixed width left justified
#> [1] "1234.6 "
fapply(a, "%08.1f") # Zero padded
#> [1] "001234.6"
fapply(a, "%+.1f") # Forced sign
#> [1] "+1234.6"
fapply(-a, "%+.1f") # Negative
#> [1] "-1234.6"
fapply(a, "%.1f%%") # Percentage
#> [1] "1234.6%"
fapply(a, "$%.2f") # Currency
#> [1] "$1234.57"
fapply(a, "The number is %f.") # Interpolation
#> [1] "The number is 1234.567890."
# "best" formatting
fapply(a, "best6") # Total width of 6
#> [1] "1234.6"
fapply(a, "best8") # Total width of 8
#> [1] "1234.568"