fmt_datetime: Format values as date-times

Description Usage Arguments Details Value Figures Function ID See Also Examples

View source: R/format_data.R

Description

Format input date-time values that are character-based and expressed according to the ISO 8601 date-time format (YYYY-MM-DD HH:MM:SS). Once the appropriate data cells are targeted with columns (and, optionally, rows), we can simply apply preset date and time styles to format the date-time values. The following date styles are available for simpler formatting of the date portion (all using the input date of 2000-02-29 in the example output dates):

  1. iso: 2000-02-29

  2. wday_month_day_year: Tuesday, February 29, 2000

  3. wd_m_day_year: Tue, Feb 29, 2000

  4. wday_day_month_year: Tuesday 29 February 2000

  5. month_day_year: February 29, 2000

  6. m_day_year: Feb 29, 2000

  7. day_m_year: 29 Feb 2000

  8. day_month_year: 29 February 2000

  9. day_month: 29 February

  10. year: 2000

  11. month: February

  12. day: 29

  13. year.mn.day: 2000/02/29

  14. y.mn.day: 0/02/29

The following time styles are available for simpler formatting of the time portion (all using the input time of 14:35:00 in the example output times):

  1. hms: 14:35:00

  2. hm: 14:35

  3. hms_p: 2:35:00 PM

  4. hm_p: 2:35 PM

  5. h_p: 2 PM

We can use the info_date_style() and info_time_style() functions as useful references for all of the possible inputs to date_style and time_style.

Usage

1
fmt_datetime(data, columns, rows = NULL, date_style = 2, time_style = 2)

Arguments

data

A table object that is created using the gt() function.

columns

The columns to format. Can either be a series of column names provided in vars(), a vector of column indices, or a helper function focused on selections. The select helper functions are: starts_with(), ends_with(), contains(), matches(), one_of(), and everything().

rows

Optional rows to format. Not providing any value results in all rows in columns being formatted. Can either be a vector of row captions provided c(), a vector of row indices, or a helper function focused on selections. The select helper functions are: starts_with(), ends_with(), contains(), matches(), one_of(), and everything(). We can also use expressions to filter down to the rows we need (e.g., [colname_1] > 100 & [colname_2] < 50).

date_style

The date style to use. Supply a number (from 1 to 14) that corresponds to the preferred date style. Use info_date_style() to see the different numbered and named date presets.

time_style

The time style to use. Supply a number (from 1 to 5) that corresponds to the preferred time style. Use info_time_style() to see the different numbered and named time presets.

Details

Targeting of values is done through columns and additionally by rows (if nothing is provided for rows then entire columns are selected). A number of helper functions exist to make targeting more effective. Conditional formatting is possible by providing a conditional expression to the rows argument. See the Arguments section for more information on this.

Value

An object of class gt_tbl.

Figures

Function ID

3-7

See Also

Other Format Data: data_color(), fmt_currency(), fmt_date(), fmt_markdown(), fmt_missing(), fmt_number(), fmt_passthrough(), fmt_percent(), fmt_scientific(), fmt_time(), fmt(), text_transform()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Use `exibble` to create a gt table;
# keep only the `datetime` column;
# format the column to have dates
# formatted as `month_day_year` and
# times to be `hms_p`
tab_1 <-
  exibble %>%
  dplyr::select(datetime) %>%
  gt() %>%
  fmt_datetime(
    columns = vars(datetime),
    date_style = 5,
    time_style = 3
  )

gt documentation built on Aug. 6, 2020, 1:07 a.m.