assign_by_interval.Rd
Function that assigns user-specified values for when a numeric variable falls within intervals defined by a set of
A numeric vector of values.
A numeric vector of values, the
breakpoints for the intervals. By default, the
lowest and highest breakpoints are set to
-Inf
and Inf
, so only the intervening
points need to be specified (this behavior can be changed).
A vector of values to assign for all cases within a given interval.
A character vector with two elements,
either '>'
or '>='
and '<'
or
'<='
.
An optional vector specifying the lowest and
and highest breakpoints. Can be set to NA
to
prevent adding to breakpoints
.
The default value to use for cases that are not within any intervals.
A vector of values.
# Default l > x <= u
x <- 1:6
assign_by_interval( x, c( 2, 4 ) )
#> [1] 1 1 2 2 3 3
# Can quickly define splits
x <- c( 1, 1, 1, 1, 2, 2, 10 )
# Mean split
assign_by_interval( x, mean(x) )
#> [1] 1 1 1 1 1 1 2
# Median split
assign_by_interval( x, median(x) )
#> [1] 1 1 1 1 2 2 2
# Custom values
assign_by_interval( x, mean(x), values = c( 'Below', 'Above' ) )
#> [1] "Below" "Below" "Below" "Below" "Below" "Below" "Above"
# Custom conditions and bounds
x <- 1:6
assign_by_interval(
x, c( 1, 2, 4, 6 ), include = c('>=', '<' ), ends = NULL
)
#> [1] 1 2 2 3 3 NA
# Can change default value for when nothing in range
assign_by_interval(x, 6, ends = c( 2, NA ), default = -1 )
#> [1] -1 -1 1 1 1 1