This is a method for the tidyr replace_na() generic. It is translated to
data.table::fcoalesce().
Note that unlike tidyr::replace_na(), data.table::fcoalesce() cannot
replace NULL values in lists.
Usage
# S3 method for class 'dtplyr_step'
replace_na(data, replace = list())Arguments
- data
A
lazy_dt().- replace
If
datais a data frame,replacetakes a named list of values, with one value for each column that has missing values to be replaced. Each value inreplacewill be cast to the type of the column indatathat it being used as a replacement in.If
datais a vector,replacetakes a single value. This single value replaces all of the missing values in the vector.replacewill be cast to the type ofdata.
Examples
library(tidyr)
# Replace NAs in a data frame
dt <- lazy_dt(tibble(x = c(1, 2, NA), y = c("a", NA, "b")))
dt %>% replace_na(list(x = 0, y = "unknown"))
#> Source: local data table [3 x 2]
#> Call: copy(`_DT35`)[, `:=`(x = fcoalesce(x, 0), y = fcoalesce(y, "unknown"))]
#>
#> x y
#> <dbl> <chr>
#> 1 1 a
#> 2 2 unknown
#> 3 0 b
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
# Replace NAs using `dplyr::mutate()`
dt %>% dplyr::mutate(x = replace_na(x, 0))
#> Source: local data table [3 x 2]
#> Call: copy(`_DT35`)[, `:=`(x = fcoalesce(x, 0))]
#>
#> x y
#> <dbl> <chr>
#> 1 1 a
#> 2 2 NA
#> 3 0 b
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
