Formatting codes for formatting strings follow the conventions of the base R strptime and sprintf functions. See below for further details.

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".

Date Formatting

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.

Numeric Formatting

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.

"best" Numeric Format

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".

See also

fapply for formatting vectors, and fdata for formatting data frames.

Examples

# Examples for formatting dates and times 
t <- Sys.time()
fapply(t, "%d/%m/%Y")              # Day/Month/Year
fapply(t, "%d%b%Y")                # Day abbreviated month year
fapply(t, "%y-%m")                 # Two digit year - month
fapply(t, "%A, %B %d")             # Weekday, unabbreviated month and date
fapply(t, "%Y-%Q")                 # Year and Quarter
fapply(t, "%Y-%m%-%d %H:%M:%S %p") # Common timestamp format

# Examples for formatting numbers
a <- 1234.56789
fapply(a, "%f")                    # Floating point number
fapply(a, "%.1f")                  # One decimal place
fapply(a, "%8.1f")                 # Fixed width
fapply(a, "%-8.1f")                # Fixed width left justified
fapply(a, "%08.1f")                # Zero padded
fapply(a, "%+.1f")                 # Forced sign
fapply(-a, "%+.1f")                # Negative 
fapply(a, "%.1f%%")                # Percentage
fapply(a, "$%.2f")                 # Currency
fapply(a, "The number is %f.")     # Interpolation

# "best" formatting
fapply(a, "best6")                 # Total width of 6
fapply(a, "best8")                 # Total width of 8