cut.Date
Create Factor by Cutting Date or POSIXt Object

Description

Creates a factor object by dividing Date or POSIXt objects into intervals with either specific cut points or the number of equal width intervals.

Usage

## S3 method for class 'Date':
cut(x, breaks, labels = NULL, start.on.monday = TRUE, right = FALSE, ...)
## S3 method for class 'POSIXt':
cut(x, breaks, labels = NULL, start.on.monday = TRUE, right = FALSE, ...)

Arguments

x a Date or POSIXt object.
breaks
  • a vector of sorted date-time breakpoints.
  • an integer specifying the number of equal-width intervals into which the data in x should be cut.
  • a character string formed by an integer followed by a space and then a time unit. The time unit can be anything that partially matches "days", "weeks", "months", "years", or "quarters" for cut.Date and "secs", "mins", "hours", "days", "weeks", "months", "years", "quarters", or "DSTdays" for cut.POSIXt.
While "days" always means 86400 seconds, "DSTdays" may be greater than or less than 86400 seconds at the boundaries of daylight saving time.
labels a character vector specifying the labels for the intervals. The default is to encode the breakpoints to make up interval names.
start.on.monday a logical value specifying if week should start on Monday or Sunday when breaks = "weeks".
right a logical value to be passed to cut.default. See details in cut.
... additional arguments.
Value
labels != FALSE a factor or ordered factor as long as x telling which group each point in x belongs to, along with an attribute, levels, which is a vector of character names for each group.
labels == FALSE an integer vector (instead of a factor) as long as x telling which group each point in x belongs to.
See Also
cut, Date, POSIXt, seq.Date
Examples
## cut.Date
dt <- seq(as.Date("2008-01-01"), as.Date("2010-12-31"), 
   length.out = 10)
cut(dt, breaks = "months")
cut(dt, breaks = "months", right = TRUE)
cut(dt, breaks = 5)
cut(dt, breaks = as.Date(c("2008-8-8", "2009-8-8", "2010-8-8")))
cut(dt, breaks = "years", labels = c("Y2008", "Y2009", "Y2010", 
   "Y2011"))
cut(dt, breaks = "years", labels = FALSE)

## cut.POSIXt plt <- seq(as.POSIXlt("2010-01-01"), as.POSIXlt("2010-12-31"), length.out = 10) cut(plt, breaks = "weeks") cut(plt, breaks = "weeks", start.on.monday = FALSE, right = TRUE) cut(plt, breaks = 5) cut(plt, breaks = as.POSIXlt(c("2010-3-1", "2010-7-8", "2010-10-30"))) cut(plt, breaks = "quarters", labels = c("Q1", "Q2", "Q3", "Q4")) cut(plt, breaks = "quarters", labels = FALSE)

## days v.s. DSTdays ## The start date-time of DST in USA 2011 is: 2011-03-13 2:00AM pct <- seq(as.POSIXct("2011-03-12"), as.POSIXct("2011-03-17"), length.out = 10) cut(pct, breaks = "days") cut(pct, breaks = "DSTdays")

Package base version 4.0.0-28
Package Index