replaceNAs,data.frame-method | R Documentation |
This function replaces NA
s in one or more data.table
, data.frame
, or matrix
columns, or in vectors, with a user-defined value.
## S4 method for signature 'data.frame'
replaceNAs(x, replace, cols = NULL)
## S4 method for signature 'matrix'
replaceNAs(x, replace, cols = NULL)
## S4 method for signature 'data.table'
replaceNAs(x, replace, cols = NULL)
## S4 method for signature 'numeric'
replaceNAs(x, replace)
## S4 method for signature 'integer'
replaceNAs(x, replace)
## S4 method for signature 'logical'
replaceNAs(x, replace)
## S4 method for signature 'character'
replaceNAs(x, replace)
x |
A |
replace |
A value of any atomic class (numeric, integer, character, Date, etc.): Value to to replace |
cols |
|
A data.table
, data.frame
, matrix
, or vector.
library(data.table)
dt <- data.table(
x = 1:10,
y = letters[1:10],
z = rnorm(10)
)
# make some values NA
dt[x == 4 | x == 8, y := NA_character_]
dt
# Replace NAs:
replaceNAs(dt, replace = -99, cols = "y")
dt
# Drop rows:
dropped <- dropRows(dt, 8:10)
dropped
# NB May not print... in that case, use:
print(dropped)
# We can also use replaceNAs() on vectors:
y <- 1:10
y[c(2, 10)] <- NA
replaceNAs(y, -99)
# Same as:
y <- 1:10
y[c(2, 10)] <- NA
y[is.na(y)] <- -99
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.