padr (version 0.5.0)

format_interval: Make a period character vector

Description

After applying thicken all the observations of a period are mapped to a single time point. This function will convert a datetime variable to a character vector that reflects the period, instead of a single time point. strftime is used to format the start and the end of the interval.

Usage

format_interval(x, start_format = "%Y-%m-%d", end_format = start_format,
  sep = " ", end_offset = 0, units_to_last = NULL)

Arguments

x

A vector of class Date, POSIXct or POSIXlt, of which the values are unique.

start_format

String to format the start values of each period, to be used in strftime.

end_format

String to format the end values of each period, to be used in strftime.

sep

Character string that separates the start_format and the end_format.

end_offset

Units in days if x is Date, or in seconds if x is POSIXct or POSIXlt. Will be subtracted from the end of each period. If 0, the end of the previous period is equal to the start of the next.

units_to_last

To determine the formatting of the last value in x, the length of the last period has to be specified. If NULL the function guesses based on the interval of x. Specify in days when x is Date, or in seconds when x is POSIXct or POSIXlt.

Value

A character vector showing the interval.

Details

The end of the periods will be determined by the next unique value in x. It does so without regarding the interval of x. If a specific interval is desired, thicken and / or pad should first be applied to create an equally spaced datetime variable.

Examples

Run this code
# NOT RUN {
library(dplyr)
library(ggplot2)
plot_set <- emergency %>%
  head(500) %>%
  thicken("hour", "h") %>%
  count(h)

# this will show the data on the full hour
ggplot(plot_set, aes(h, n)) + geom_col()

# adding a character to indicate the hours of the interval.
plot_set %>%
  mutate(h_int = format_interval(h, "%H", sep = "-"))
# }

Run the code above in your browser using DataCamp Workspace