Extended cutting function
Usage
# S3 method for class 'hms'
cut(x, breaks, ...)
# S3 method for class 'POSIXt'
cut(
x,
breaks,
right = FALSE,
include.lowest = TRUE,
start.on.monday = TRUE,
...
)
# S3 method for class 'POSIXct'
cut(
x,
breaks,
right = FALSE,
include.lowest = TRUE,
start.on.monday = TRUE,
...
)
# S3 method for class 'Date'
cut(x, breaks, start.on.monday = TRUE, ...)
Examples
readr::parse_time(c("01:00:20", "03:00:20", "01:20:20", "08:20:20", "21:20:20", "03:02:20")) |> cut(2)
#> [1] 01:00:20 01:00:20 01:00:20 03:01:20 <NA> 03:01:20
#> attr(,"brks")
#> 01:00:20
#> 03:01:20
#> 21:20:20
#> Levels: 01:00:20 03:01:20
readr::parse_time(c("01:00:20", "03:00:20", "01:20:20", "08:20:20", "21:20:20", "03:02:20")) |> cut("min")
#> [1] 01:00:00 03:00:00 01:20:00 08:20:00 21:20:00 03:02:00
#> attr(,"brks")
#> 01:00:00
#> 01:20:00
#> 03:00:00
#> 03:02:00
#> 08:20:00
#> 21:20:00
#> 21:20:20
#> Levels: 01:00:00 01:20:00 03:00:00 03:02:00 08:20:00 21:20:00
readr::parse_time(c("01:00:20", "03:00:20", "01:20:20", "08:20:20", "21:20:20", "03:02:20")) |> cut(breaks = "hour")
#> [1] 01:00:00 03:00:00 01:00:00 08:00:00 21:00:00 03:00:00
#> attr(,"brks")
#> 01:00:00
#> 03:00:00
#> 08:00:00
#> 21:00:00
#> 21:20:20
#> Levels: 01:00:00 03:00:00 08:00:00 21:00:00
readr::parse_time(c("01:00:20", "03:00:20", "01:20:20", "08:20:20", "21:20:20", "03:02:20")) |> cut(breaks = hms::as_hms(c("01:00:00", "03:01:20", "9:20:20")))
#> [1] 01:00:00 01:00:00 01:00:00 03:01:20 <NA> 03:01:20
#> attr(,"brks")
#> 01:00:00
#> 03:01:20
#> 09:20:20
#> Levels: 01:00:00 03:01:20
d_t <- readr::parse_time(c("01:00:20", "03:00:20", "01:20:20", "03:02:20", NA))
f <- d_t |> cut(2)
readr::parse_time(c("01:00:20", "03:00:20", "01:20:20", "03:02:20", NA)) |> cut(breaks = lubridate::as_datetime(c(hms::as_hms(levels(f)), hms::as_hms(max(d_t, na.rm = TRUE) + 1))), right = FALSE)
#> [1] 01:00:20 02:10:20 01:00:20 02:10:20 <NA>
#> attr(,"brks")
#> 01:00:20
#> 02:10:20
#> 03:02:21
#> Levels: 01:00:20 02:10:20
readr::parse_datetime(c("1992-02-01 01:00:20", "1992-02-06 03:00:20", "1992-05-01 01:20:20", "1992-09-01 08:20:20", "1999-02-01 21:20:20", "1992-12-01 03:02:20")) |> cut(2)
#> [1] 1992-02-01 01:00:20 1992-02-01 01:00:20 1992-02-01 01:00:20
#> [4] 1992-02-01 01:00:20 1999-02-01 21:20:20 1992-02-01 01:00:20
#> Levels: 1992-02-01 01:00:20 1999-02-01 21:20:20
readr::parse_datetime(c("1992-02-01 01:00:20", "1992-02-06 03:00:20", "1992-05-01 01:20:20", "1992-09-01 08:20:20", "1999-02-01 21:20:20", "1992-12-01 03:02:20")) |> cut(breaks="weekday")
#> Error in cut.POSIXt(readr::parse_datetime(c("1992-02-01 01:00:20", "1992-02-06 03:00:20", "1992-05-01 01:20:20", "1992-09-01 08:20:20", "1999-02-01 21:20:20", "1992-12-01 03:02:20")), breaks = "weekday"): invalid specification of 'breaks'
readr::parse_datetime(c("1992-02-01 01:00:20", "1992-02-06 03:00:20", "1992-05-01 01:20:20", "1992-09-01 08:20:20", "1999-02-01 21:20:20", "1992-12-01 03:02:20")) |> cut(breaks="month_only")
#> Error in cut.POSIXt(readr::parse_datetime(c("1992-02-01 01:00:20", "1992-02-06 03:00:20", "1992-05-01 01:20:20", "1992-09-01 08:20:20", "1999-02-01 21:20:20", "1992-12-01 03:02:20")), breaks = "month_only"): invalid specification of 'breaks'
as.Date(c("1992-02-01 01:00:20", "1992-02-06 03:00:20", "1992-05-01 01:20:20", "1992-09-01 08:20:20", "1999-02-01 21:20:20", "1992-12-01 03:02:20")) |> cut(2)
#> [1] 1992-02-01 1992-02-01 1992-02-01 1992-02-01 1999-02-01 1992-02-01
#> Levels: 1992-02-01 1999-02-01
as.Date(c("1992-02-01 01:00:20", "1992-02-06 03:00:20", "1992-05-01 01:20:20", "1992-09-01 08:20:20", "1999-02-01 21:20:20", "1992-12-01 03:02:20")) |> cut(breaks="weekday")
#> Error in cut.Date(as.Date(c("1992-02-01 01:00:20", "1992-02-06 03:00:20", "1992-05-01 01:20:20", "1992-09-01 08:20:20", "1999-02-01 21:20:20", "1992-12-01 03:02:20")), breaks = "weekday"): invalid specification of 'breaks'