numform (version 0.5.0)

f_quarter: Format Quarters

Description

Format long/abbreviation month name, integer, or date formats to a quarter format (i.e., Q1, Q2, Q3, Q4).

Usage

f_quarter(x, prefix = "Q", space = "", max = 12, ...)

# S3 method for default f_quarter(x, prefix = "Q", space = "", max = 12, ...)

# S3 method for numeric f_quarter(x, prefix = "Q", space = "", max = ifelse(all(x %in% c(1:4, NA)), 4, 12), ...)

# S3 method for Date f_quarter(x, prefix = "Q", space = "", max = 12, ...)

# S3 method for POSIXt f_quarter(x, prefix = "Q", space = "", max = 12, ...)

# S3 method for hms f_quarter(x, prefix = "Q", space = "", max = 12, ...)

ff_quarter(prefix = "Q", space = "", max = 12, ...)

Arguments

x

A vector of month names, integers 1-12, or dates.

prefix

A quarter prefix (defaults to 'Q').

space

A string to place between `Q` and quarter number.

max

A maximum in the x vector, if x is numeric, corresponding to months (12) or quarters (4).

ignored.

Value

Returns a quarter formatted atomic vector.

Examples

Run this code
# NOT RUN {
f_quarter(month.name)

f_quarter(1:12)

dates <- seq(as.Date("2000/1/1"), by = "month", length.out = 12)
f_quarter(dates)
# }
# NOT RUN {
if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse)

set.seed(10)
dat <- data_frame(
    month = sample(month.name, 1000, TRUE),
    area =  sample(LETTERS[1:5], 1000, TRUE)
) %>%
    mutate(quarter = factor(f_quarter(month), levels = constant_quarters)) %>%
    count(quarter, area)

ggplot(dat, aes(quarter, n)) +
    geom_bar(stat = 'identity') +
    facet_wrap(~ area)
# }

Run the code above in your browser using DataLab