f_year: Format Years

Description Usage Arguments Value Examples

View source: R/f_year.R

Description

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

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
f_year(x, digits = 2, ...)

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

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

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

## S3 method for class '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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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))

## End(Not run)

numform documentation built on Oct. 10, 2021, 1:10 a.m.