# 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)
Run the code above in your browser using DataLab