| Title: | A Fancy Version of base::cut() |
|---|---|
| Description: | Provides two functions fancycut() and wafflecut(), which essentially do the same thing, with slightly different syntax. These function allow for turning a numeric vector into a factor, like cut(). However, unlike cut() they give the user more control over how to specify the intervals. |
| Authors: | Adam Rich [aut, cre, cph] (ORCID: <https://orcid.org/0009-0007-0992-1956>), Richie Cotton [ctb] |
| Maintainer: | Adam Rich <[email protected]> |
| License: | CC0 |
| Version: | 0.1.4 |
| Built: | 2026-10-08 17:35:54 UTC |
| Source: | https://github.com/adamleerich/fancycut |
cut, turn a vector of numbers into a factorLike cut, turn a vector of numbers into a factor
fancycut(x, na.bucket = NA, unmatched.bucket = NA, out.as.factor = TRUE, ...)fancycut(x, na.bucket = NA, unmatched.bucket = NA, out.as.factor = TRUE, ...)
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 |
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')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')
cut, turn a vector of numbers into a factorLike cut, turn a vector of numbers into a factor
wafflecut( x, intervals, buckets = intervals, na.bucket = NA, unmatched.bucket = NA, out.as.factor = TRUE )wafflecut( x, intervals, buckets = intervals, na.bucket = NA, unmatched.bucket = NA, out.as.factor = TRUE )
x |
a numeric vector |
intervals |
a character vector of intervals |
buckets |
a character vector of levels for the new factor
these have a 1-1 correspondence with |
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. |
wafflecut(-10:10, c('[0,2)','[2,5)','[5,10]'), c('Small','Medium','Large')) wafflecut(-10:10, c('[0,0]','(0,2]','(2,5)','[5,10]'), c('Zero','Small','Medium','Large')) wafflecut(-10:10, c('[0,2)','[2,5)','[5,10]'), c('Small','Medium','Large')) wafflecut(-10:10, c('[0,0]','[0,2]','(2,5)','[5,10]'), c('Zero','Small','Medium','Large')) # The following examples are from Richie Cotton via # https://www.rdocumentation.org/packages/fancycut/versions/0.1.1/topics/fancycut # Not all the values have to live in a bucket. x <- seq.int(0, 1, 0.25) wafflecut(x, c('(0.2, 0.3)', '(0.7, 0.8)'), c('low', 'high')) # You can use unmatched.bucket to deal with these other intervals. x <- seq.int(0, 1, 0.25) wafflecut(x, c('(0.2, 0.3)', '(0.7, 0.8)'), c('low', 'high'), unmatched.bucket = 'other') # To match NA values, use na.bucket. x2 <- c(seq.int(0, 1, 0.25), NA) wafflecut(x2, c('[0, 0.5)', '[0.5, 1]'), c('low', 'high'), na.bucket = 'missing')wafflecut(-10:10, c('[0,2)','[2,5)','[5,10]'), c('Small','Medium','Large')) wafflecut(-10:10, c('[0,0]','(0,2]','(2,5)','[5,10]'), c('Zero','Small','Medium','Large')) wafflecut(-10:10, c('[0,2)','[2,5)','[5,10]'), c('Small','Medium','Large')) wafflecut(-10:10, c('[0,0]','[0,2]','(2,5)','[5,10]'), c('Zero','Small','Medium','Large')) # The following examples are from Richie Cotton via # https://www.rdocumentation.org/packages/fancycut/versions/0.1.1/topics/fancycut # Not all the values have to live in a bucket. x <- seq.int(0, 1, 0.25) wafflecut(x, c('(0.2, 0.3)', '(0.7, 0.8)'), c('low', 'high')) # You can use unmatched.bucket to deal with these other intervals. x <- seq.int(0, 1, 0.25) wafflecut(x, c('(0.2, 0.3)', '(0.7, 0.8)'), c('low', 'high'), unmatched.bucket = 'other') # To match NA values, use na.bucket. x2 <- c(seq.int(0, 1, 0.25), NA) wafflecut(x2, c('[0, 0.5)', '[0.5, 1]'), c('low', 'high'), na.bucket = 'missing')