Skip to content

Commit

Permalink
Wrote quarter accessor, which returns a date's quarter as a number.
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettgman committed Oct 2, 2012
1 parent 9c0c451 commit 453d056
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ Collate:
'guess.r'
'stamp.r'
'ops-%m+%.r'
'accessors-quarter.r'
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export(pretty.sec)
export(pretty.unit)
export(pretty.year)
export(pretty_dates)
export(quarter)
export(reclass_date)
export(reclass_timespan)
export(rollback)
Expand Down
36 changes: 36 additions & 0 deletions R/accessors-quarter.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#' @include accessors-month.r
NULL

#' Get the fiscal quarter of a date-time.
#'
#' Fiscal quarters are a way of dividing the year into fourths. The first quarter (Q1)
#' comprises January, February and March; the second quarter (Q2) comprises April, May,
#' June; the third quarter (Q3) comprises July, August, September; the fourth quarter (Q4)
#' October, November, December.
#'
#' @param x a date-time object of class POSIXct, POSIXlt, Date, chron, yearmon, yearqtr, zoo,
#' zooreg, timeDate, xts, its, ti, jul, timeSeries, fts or anything else that can be converted
#' with as.POSIXlt
#' @keywords utilities manip chron methods
#' @return numeric the fiscal quarter that the date-time occurs in
#' @examples
#' x <- ymd(c("2012-03-26", "2012-05-04", "2012-09-23", "2012-12-31"))
#' # 1 2 3 4
#' @export
quarter <- function(x) {
m <- month(x)
quarters <- c("1" = 1,
"2" = 1,
"3" = 1,
"4" = 2,
"5" = 2,
"6" = 2,
"7" = 3,
"8" = 3,
"9" = 3,
"10" = 4,
"11" = 4,
"12" = 4)
unname(quarters[m])
}

32 changes: 32 additions & 0 deletions man/quarter.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
\name{quarter}
\alias{quarter}
\title{Get the fiscal quarter of a date-time.}
\usage{
quarter(x)
}
\arguments{
\item{x}{a date-time object of class POSIXct, POSIXlt,
Date, chron, yearmon, yearqtr, zoo, zooreg, timeDate,
xts, its, ti, jul, timeSeries, fts or anything else that
can be converted with as.POSIXlt}
}
\value{
numeric the fiscal quarter that the date-time occurs in
}
\description{
Fiscal quarters are a way of dividing the year into
fourths. The first quarter (Q1) comprises January,
February and March; the second quarter (Q2) comprises
April, May, June; the third quarter (Q3) comprises July,
August, September; the fourth quarter (Q4) October,
November, December.
}
\examples{
x <- ymd(c("2012-03-26", "2012-05-04", "2012-09-23", "2012-12-31"))
# 1 2 3 4
}
\keyword{chron}
\keyword{manip}
\keyword{methods}
\keyword{utilities}

0 comments on commit 453d056

Please sign in to comment.