fancycut
Like cut
, turn a vector of numbers into a factor
Like cut
, turn a vector of numbers into a factor
Usage
fancycut(x, na.bucket = NA, unmatched.bucket = NA, out.as.factor = TRUE,
...)
Arguments
- x
a numeric vector
- na.bucket
what level should NA values be given?
- unmatched.bucket
what level should numbers not covered by an interval be given?
- out.as.factor
default is TRUE Should the resulting vector be a factor? If FALSE will return a character vector.
- ...
These take the form
tag = value
. Tags become the bucket names and values the interval definitions.
Examples
# NOT RUN {
fancycut(
x = -10:10,
Zero = 0,
Small = '[0,2)',
Medium = '[2,5]',
Large = '(5,10]'
)
# The following examples are from Richie Cotton via
# https://www.rdocumentation.org/packages/fancycut/versions/0.1.1/topics/fancycut
# The tag = value syntax is useful.
x <- seq.int(0, 1, 0.25)
fancycut(x, low = '[0, 0.5]', high = '(0.5, 1]')
# Not all the values have to live in a bucket.
x <- seq.int(0, 1, 0.25)
fancycut(x, low = '(0.2, 0.3]', high = '(0.7, 0.8)')
# You can use unmatched.bucket to deal with these other intervals.
x <- seq.int(0, 1, 0.25)
fancycut(x, low = '(0.2, 0.3]', high = '(0.7, 0.8)', unmatched.bucket = 'other')
# To match a specific value, make the lower and upper bound the same number.
x <- seq.int(0, 1, 0.25)
fancycut(x, low = '[0, 0.5)', half = '[0.5,0.5]', high = '(0.5, 1]')
# To match NA values, use na.bucket.
x2 <- c(seq.int(0, 1, 0.25), NA)
fancycut(x2, low = '[0, 0.5)', high = '[0.5, 1]', na.bucket = 'missing')
# }
Community examples
The `tag = value` syntax is also useful. ```r x <- seq.int(0, 1, 0.25) fancycut(x, low = '[0, 0.5]', high = '(0.5, 1]') ## [1] low low low high high ## Levels: low high <NA> ``` By default it adds an `NA` level, which you may not want. Turn this off using `na.bucket = NULL` and `unmatched.bucket = NULL`. ```r x <- seq.int(0, 1, 0.25) fancycut(x, low = '[0, 0.5]', high = '(0.5, 1]', na.bucket = NULL, unmatched.bucket = NULL) ## [1] low low low high high ## Levels: low high ``` Not all the values have to live in a bucket. ```r x <- seq.int(0, 1, 0.25) fancycut(x, low = '(0.2, 0.3)', high = '(0.7, 0.8)') ## [1] <NA> low <NA> high <NA> ## Levels: low high <NA> ``` You can use `unmatched.bucket` to deal with these other intervals. ```r x <- seq.int(0, 1, 0.25) fancycut(x, low = '(0.2, 0.3)', high = '(0.7, 0.8)', unmatched.bucket = 'other') [1] other low other high other Levels: low high <NA> other ``` To match a specific value, make the lower and upper bound the same number. ```r x <- seq.int(0, 1, 0.25) fancycut(x, low = '[0, 0.5)', half = '[0.5,0.5]', high = '(0.5, 1]') ## [1] low low half high high ## Levels: low half high <NA> ``` To match `NA` values, use `na.bucket`. ```r x2 <- c(seq.int(0, 1, 0.25), NA) fancycut(x2, low = '[0, 0.5]', high = '(0.5, 1]', na.bucket = 'missing') ## [1] low low low high high missing ## Levels: low high missing <NA> ```