Learn R Programming

numform (version 0.6.4)

f_12_hour: Format 12 Hour Times

Description

Format times to the typical 12 hour '

Usage

f_12_hour(x = Sys.time(), format = "%I:%M %p", pad.char = "", ...)

# S3 method for default f_12_hour(x, format = "%I:%M %p", pad.char = "", ...)

# S3 method for integer f_12_hour(x, format = "%I:%M %p", pad.char = "", ...)

# S3 method for numeric f_12_hour(x, format = "%I:%M %p", pad.char = "", ...)

# S3 method for hms f_12_hour(x, format = "%I:%M %p", pad.char = "", ...)

ff_12_hour(format = "%I:%M %p", pad.char = "", ...)

Arguments

x

A vector of coercible times.

format

A character string specifying the time output format.

pad.char

A character to use for leading padding if lengths of output are unequal.

Other arguments passed to as.POSIXct.

Value

Returns a string of publication ready 12 hour time stamps.

Examples

Run this code
# NOT RUN {
f_12_hour(Sys.time())
f_12_hour(Sys.time(), pad.char ='0')
f_12_hour(Sys.time(), pad.char =' ')
f_12_hour(Sys.time(), '%I:%M:%S %p')
f_12_hour(c(NA, 0:24), '%I %p')
set.seed(10)
times <- as.POSIXct(sample(seq_len(1e4), 12), origin = '1970-01-01')
paste(f_12_hour(range(times)), collapse = ' to ')
# }
# NOT RUN {
library(tidyverse)

set.seed(10)
data_frame(
    time = as.POSIXct(sample(seq_len(1e4), 12), origin = '1970-01-01'),
    val = sample(1:20, length(time), TRUE)
) %>%
    mutate(prop = val/sum(val)) %>%
    ggplot(aes(time, prop)) +
        geom_line() +
        scale_x_time(labels = ff_12_hour(format = '%I %p')) +
        scale_y_continuous(labels = ff_prop2percent(digits = 0))
# }

Run the code above in your browser using DataLab