Examples for 'base::cut.POSIXt'


Convert a Date or Date-Time Object to a Factor

Aliases: cut.POSIXt cut.Date

Keywords: manip chron

### ** Examples

## random dates in a 10-week period
cut(ISOdate(2001, 1, 1) + 70*86400*stats::runif(100), "weeks")
  [1] 2001-02-26 2001-02-05 2001-01-15 2001-02-12 2001-01-08 2001-01-01
  [7] 2001-01-15 2001-03-05 2001-02-19 2001-02-12 2001-03-05 2001-01-29
 [13] 2001-02-05 2001-01-29 2001-01-22 2001-03-12 2001-01-29 2001-02-26
 [19] 2001-02-19 2001-01-08 2001-02-26 2001-02-05 2001-01-01 2001-02-05
 [25] 2001-01-08 2001-01-08 2001-03-12 2001-01-29 2001-02-12 2001-03-12
 [31] 2001-02-19 2001-03-05 2001-01-22 2001-02-05 2001-01-15 2001-02-12
 [37] 2001-02-19 2001-02-05 2001-01-22 2001-02-26 2001-01-08 2001-01-15
 [43] 2001-02-19 2001-02-19 2001-01-29 2001-01-01 2001-02-05 2001-01-15
 [49] 2001-01-01 2001-01-08 2001-02-19 2001-03-05 2001-03-05 2001-01-01
 [55] 2001-01-29 2001-01-01 2001-01-15 2001-02-19 2001-03-05 2001-02-12
 [61] 2001-01-01 2001-02-12 2001-01-29 2001-01-15 2001-01-15 2001-03-05
 [67] 2001-03-05 2001-02-12 2001-03-05 2001-01-01 2001-02-19 2001-02-26
 [73] 2001-02-05 2001-01-01 2001-01-01 2001-02-05 2001-03-05 2001-02-12
 [79] 2001-02-26 2001-01-22 2001-01-08 2001-02-12 2001-02-12 2001-01-08
 [85] 2001-01-22 2001-01-01 2001-03-05 2001-02-26 2001-02-05 2001-01-22
 [91] 2001-01-29 2001-01-08 2001-03-05 2001-03-05 2001-02-05 2001-02-05
 [97] 2001-01-15 2001-01-22 2001-01-08 2001-02-05
11 Levels: 2001-01-01 2001-01-08 2001-01-15 2001-01-22 ... 2001-03-12
cut(as.Date("2001/1/1") + 70*stats::runif(100), "weeks")
  [1] 2001-03-05 2001-02-05 2001-01-01 2001-02-12 2001-02-05 2001-01-29
  [7] 2001-02-12 2001-02-19 2001-01-01 2001-02-12 2001-02-05 2001-01-22
 [13] 2001-01-29 2001-01-15 2001-02-12 2001-02-05 2001-01-29 2001-01-01
 [19] 2001-01-01 2001-01-29 2001-03-05 2001-02-12 2001-02-05 2001-01-29
 [25] 2001-03-05 2001-02-26 2001-01-22 2001-01-29 2001-03-05 2001-02-12
 [31] 2001-02-12 2001-03-05 2001-02-19 2001-01-22 2001-02-12 2001-02-19
 [37] 2001-01-01 2001-01-22 2001-02-26 2001-02-19 2001-01-22 2001-02-26
 [43] 2001-03-05 2001-02-26 2001-01-01 2001-01-15 2001-01-01 2001-01-08
 [49] 2001-01-22 2001-01-29 2001-01-15 2001-02-12 2001-02-05 2001-02-26
 [55] 2001-01-22 2001-01-08 2001-02-05 2001-01-08 2001-01-29 2001-02-12
 [61] 2001-02-12 2001-01-08 2001-02-12 2001-01-29 2001-01-22 2001-01-29
 [67] 2001-03-05 2001-01-22 2001-02-05 2001-03-05 2001-01-08 2001-01-29
 [73] 2001-02-19 2001-01-08 2001-01-08 2001-01-08 2001-01-29 2001-02-12
 [79] 2001-02-26 2001-01-08 2001-02-19 2001-02-12 2001-01-08 2001-01-01
 [85] 2001-02-12 2001-01-15 2001-01-29 2001-01-08 2001-01-29 2001-03-05
 [91] 2001-02-19 2001-02-12 2001-02-19 2001-03-05 2001-01-29 2001-01-22
 [97] 2001-01-15 2001-01-15 2001-02-05 2001-02-19
10 Levels: 2001-01-01 2001-01-08 2001-01-15 2001-01-22 ... 2001-03-05
# The standards all have midnight as the start of the day, but some
# people incorrectly interpret it at the end of the previous day ...
tm <- seq(as.POSIXct("2012-06-01 06:00"), by = "6 hours", length.out = 24)
aggregate(1:24, list(day = cut(tm, "days")), mean)
         day    x
1 2012-06-01  2.0
2 2012-06-02  5.5
3 2012-06-03  9.5
4 2012-06-04 13.5
5 2012-06-05 17.5
6 2012-06-06 21.5
7 2012-06-07 24.0
# and a version with midnight included in the previous day:
aggregate(1:24, list(day = cut(tm, "days", right = TRUE)), mean)
         day    x
1 2012-06-01  2.5
2 2012-06-02  6.5
3 2012-06-03 10.5
4 2012-06-04 14.5
5 2012-06-05 18.5
6 2012-06-06 22.5

[Package base version 4.2.3 Index]