View source: R/S03_Utilities.R
assign_by_interval | R Documentation |
Function that assigns user-specified values for when a numeric variable falls within intervals defined by a set of
assign_by_interval(
x,
breakpoints,
values = NULL,
include = c(">", "<="),
ends = c(-Inf, Inf),
default = NA
)
x |
A numeric vector of values. |
breakpoints |
A numeric vector of values, the
breakpoints for the intervals. By default, the
lowest and highest breakpoints are set to
|
values |
A vector of values to assign for all cases within a given interval. |
include |
A character vector with two elements,
either |
ends |
An optional vector specifying the lowest and
and highest breakpoints. Can be set to |
default |
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 ) )
# Can quickly define splits
x <- c( 1, 1, 1, 1, 2, 2, 10 )
# Mean split
assign_by_interval( x, mean(x) )
# Median split
assign_by_interval( x, median(x) )
# Custom values
assign_by_interval( x, mean(x), values = c( 'Below', 'Above' ) )
# Custom conditions and bounds
x <- 1:6
assign_by_interval(
x, c( 1, 2, 4, 6 ), include = c('>=', '<' ), ends = NULL
)
# Can change default value for when nothing in range
assign_by_interval(x, 6, ends = c( 2, NA ), default = -1 )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.