Group values together that belong to the same intervals being defined by breaks
Usage
groupByBreaks(
x,
breaks,
values = breaksToIntervalLabels(breaks),
right = TRUE,
add.Inf.limits = TRUE,
to.factor = FALSE,
columns = NULL,
keyFields = NULL
)
Arguments
- x
vector of values or a data frame. If
x
is a data frame, the function is applied to each column given incolumns
(all numeric columns by default)- breaks
vector of breaks
- values
values to be assigned
- right
if TRUE the intervals are right-closed, else left-closed.
- add.Inf.limits
if TRUE (default), -Inf and Inf are added to the left and right, respectively, of
breaks
- to.factor
if
TRUE
the new values are converted tofactor
. The default isFALSE
.- columns
NULL
or vector of column names (ifx
is a data frame)- keyFields
NULL
or vector of column names (ifx
is a data frame). If notNULL
, a data frame with these columns coming first and the interval labels in the last column is returned.
Examples
groupByBreaks(1:10, breaks = 5, values = c("<= 5", "> 5"))
#> [1] "<= 5" "<= 5" "<= 5" "<= 5" "<= 5" "> 5" "> 5" "> 5" "> 5" "> 5"
groupByBreaks(1:10, breaks = 5, right = FALSE, values = c("< 5", ">= 5"))
#> [1] "< 5" "< 5" "< 5" "< 5" ">= 5" ">= 5" ">= 5" ">= 5" ">= 5" ">= 5"
# Prepare a simple data frame
x <- kwb.utils::noFactorDataFrame(
id = c("A", "B", "C"),
value = c(10, 20, 30)
)
# Keep the ID column of the data frame
groupByBreaks(x, breaks = 20, keyFields = "id")
#> id value
#> 1 A <= 20
#> 2 B <= 20
#> 3 C > 20