f_quarter: Format Quarters

Description Usage Arguments Value Examples

View source: R/f_quarter.R

Description

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

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
f_quarter(x, prefix = "Q", space = "", max = 12, ...)

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

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

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

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

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

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

## End(Not run)

Example output

 [1] "Q1" "Q1" "Q1" "Q2" "Q2" "Q2" "Q3" "Q3" "Q3" "Q4" "Q4" "Q4"
 [1] "Q1" "Q1" "Q1" "Q2" "Q2" "Q2" "Q3" "Q3" "Q3" "Q4" "Q4" "Q4"
 [1] "Q1" "Q1" "Q1" "Q2" "Q2" "Q2" "Q3" "Q3" "Q3" "Q4" "Q4" "Q4"
Loading required package: pacman
Warning message:
`data_frame()` is deprecated as of tibble 1.1.0.
Please use `tibble()` instead.
This warning is displayed once every 8 hours.
Call `lifecycle::last_warnings()` to see where this warning was generated. 

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