feat: new cut option to simplify factors to only the top N levels

This commit is contained in:
Andreas Gammelgaard Damsbo 2025-10-21 21:30:07 +03:00
commit 0c2b061708
No known key found for this signature in database
12 changed files with 337 additions and 236 deletions

View file

@ -1,5 +1,5 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/cut-variable-dates.R
% Please edit documentation in R/cut_var.R
\name{cut_var}
\alias{cut_var}
\alias{cut_var.default}
@ -7,6 +7,7 @@
\alias{cut_var.POSIXt}
\alias{cut_var.POSIXct}
\alias{cut_var.Date}
\alias{cut_var.factor}
\title{Extended cutting function with fall-back to the native base::cut}
\usage{
cut_var(x, ...)
@ -34,17 +35,25 @@ cut_var(x, ...)
)
\method{cut_var}{Date}(x, breaks = NULL, start.on.monday = TRUE, ...)
\method{cut_var}{factor}(x, breaks = NULL, type = c("top", "bottom"), other = "Other", ...)
}
\arguments{
\item{x}{an object inheriting from class "POSIXct"}
\item{...}{passed on}
\item{type}{}
}
\value{
factor
factor
}
\description{
Extended cutting function with fall-back to the native base::cut
Simplify a factor to only the top or bottom n levels
}
\examples{
readr::parse_time(c("01:00:20", "03:00:20", "01:20:20", "08:20:20", "21:20:20", "03:02:20")) |> cut_var(2)
@ -57,9 +66,13 @@ readr::parse_time(c("01:00:20", "03:00:20", "01:20:20", "03:02:20", NA)) |> cut_
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_var(2)
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_var(breaks = "weekday")
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_var(breaks = "month_only")
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_var(breaks=NULL,format = "\%A-\%H")
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_var(breaks=NULL,format = "\%W")
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_var(breaks = NULL, format = "\%A-\%H")
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_var(breaks = NULL, format = "\%W")
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_var(2)
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_var(breaks = "weekday")
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_var(format = "\%W")
mtcars$carb |>
as.factor() |>
cut_var(2) |>
table()
}