Learn R Programming

numform (version 0.7.0)

f_year: Format Years

Description

Format 4 digit integer, date, or POSIXlt formats to 2 or 4 digit years.

Usage

f_year(x, digits = 2, ...)

# S3 method for numeric f_year(x, digits = 2, ...)

# S3 method for Date f_year(x, digits = 2, ...)

# S3 method for POSIXt f_year(x, digits = 2, ...)

# S3 method for hms f_year(x, digits = 2, ...)

ff_year(digits = 2, ...)

Arguments

x

A vector of 4 digits integers, dates, or POSIXlt.

digits

Either 2 or 4 for the number of digits to make the year.

ignored.

Value

Returns a vector of two or four digit years.

Examples

Run this code
# NOT RUN {
f_year(as.Date(paste0(1998:2016, '-12-12')))
f_year(c(NA, 1998:2016, 21345))
# }
# NOT RUN {
library(tidyverse)

dat <- data_frame(
    year = 1998:2016,
    year2 = as.POSIXct(sample(seq_len(1e4), 12), origin = '1970-01-01') +
        (365 * 24 * 3600 * seq_len(19)),
    val = sample(1:20, length(year), TRUE)
) %>%
    mutate(prop = val/sum(val))

dat %>%
    ggplot(aes(year, prop)) +
        geom_line() +
        scale_x_continuous(labels = ff_year(digits = 2), breaks = 1998:2016) +
        scale_y_continuous(labels = ff_prop2percent(digits = 0))

dat %>%
    ggplot(aes(year2, prop)) +
        geom_line() +
        scale_x_time(labels = ff_year(digits = 2), breaks = dat$year2) +
        scale_y_continuous(labels = ff_prop2percent(digits = 0))
# }

Run the code above in your browser using DataLab