Permalink
Browse files

Wrote quarter accessor, which returns a date's quarter as a number.

  • Loading branch information...
1 parent 9c0c451 commit 453d056f5a01a28175355675ba530490bb140f3c @garrettgman garrettgman committed Oct 2, 2012
Showing with 70 additions and 0 deletions.
  1. +1 −0 DESCRIPTION
  2. +1 −0 NAMESPACE
  3. +36 −0 R/accessors-quarter.r
  4. +32 −0 man/quarter.Rd
View
@@ -67,3 +67,4 @@ Collate:
'guess.r'
'stamp.r'
'ops-%m+%.r'
+ 'accessors-quarter.r'
View
@@ -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)
View
@@ -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])
+}
+
View
@@ -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.