set_missing | R Documentation |
Quickly set all missing values to indicated value.
set_missing(data, value, exclude = NULL)
data |
input data, in data.table format only. |
value |
a single value or a list of two values to be set to. See 'Details'. |
exclude |
column index or name to be excluded. |
The class of value
will determine what type of columns to be set, e.g., if value
is 0, then missing values for continuous features will be set.
When supplying a list of two values, only one numeric and one non-numeric is allowed.
This function updates data.table object directly. Otherwise, output data will be returned matching input object class.
# Load packages
library(data.table)
# Generate missing values in iris data
dt <- data.table(iris)
for (j in 1:4) set(dt, i = sample.int(150, j * 30), j, value = NA_integer_)
set(dt, i = sample.int(150, 25), 5L, value = NA_character_)
# Set all missing values to 0L and unknown
dt2 <- copy(dt)
set_missing(dt2, list(0L, "unknown"))
# Set missing numerical values to 0L
dt3 <- copy(dt)
set_missing(dt3, 0L)
# Set missing discrete values to unknown
dt4 <- copy(dt)
set_missing(dt4, "unknown")
# Set missing values excluding some columns
dt5 <- copy(dt)
set_missing(dt4, 0L, 1L:2L)
set_missing(dt4, 0L, names(dt5)[3L:4L])
# Return from non-data.table input
set_missing(airquality, 999999L)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.