cut_with_nas: Convert numeric to factor, with an explicit level for 'NA's

View source: R/cut-wth-nas.R

cut_with_nasR Documentation

Convert numeric to factor, with an explicit level for NAs

Description

Like base::cut(), but creates a level representing the missing values.

Usage

cut_with_nas(x, .missing = "Unknown", ...)

Arguments

x

A numeric or integer vector to cut into factor levels. Required.

.missing

The name of the level representing the NA values within x.

...

further arguments passed to base::cut().

Value

A factor.

Note

Discussed in the Stack Overflow question, "cut() a variable with missing values"

Author(s)

Will Beasley

Examples

w      <- c(0L, NA_integer_, 22:25, NA_integer_, 40)
breaks <- c(0, 25, 50)
labels <- c("lower", "upper")

cut_with_nas(w, breaks=2)
cut_with_nas(w, breaks=breaks, labels=labels)
cut_with_nas(w, breaks=breaks               )
cut_with_nas(w, breaks=breaks               , include.lowest=TRUE)
cut_with_nas(w, breaks=breaks               , include.lowest=TRUE, right=FALSE)
cut_with_nas(w, breaks=breaks                                    , right=FALSE)

OuhscBbmc/OuhscMunge documentation built on March 2, 2024, 11:44 a.m.