This is a generic function to replace NA values in data. It takes most data types as input and is extendible by other packages.

na_replace(x, ...)

# S3 method for default
na_replace(x, replacement = "", ...)

# S3 method for data.frame
na_replace(x, ..., replacement = NULL)

# S3 method for matrix
na_replace(x, replacement = 0, ...)

# S3 method for list
na_replace(x, replacement = NULL, ...)

# S3 method for numeric
na_replace(x, replacement = 0, ...)

# S3 method for Date
na_replace(x, replacement = Sys.Date(), ...)

# S3 method for logical
na_replace(x, replacement = FALSE, ...)

Arguments

x

any vector, data.frame, matrix or list with values of which NA must be replaced

...

When x is a data.frame: columns of x to affect. This supports tidy evaluation without the need to quote the columns, see Examples.

replacement

value to replace NA with. This is at default: 0 for numeric values and class matrix, FALSE for class logical, today for class Date, and "" otherwise. Can also be a vector with the length of the number of NAs of x (sum(is.na(x))). When x is a data.frame, this can be a vector with the length of the number of columns to be affected, see Examples.

Details

All functions preserve attributes. Within a list or data.frame, all attributes per index/item/column are also preserved.

Examples

mtrx <- matrix(c(1, 2, NA, 3), nrow = 2)
mtrx
#>      [,1] [,2]
#> [1,]    1   NA
#> [2,]    2    3
na_replace(mtrx)
#>      [,1] [,2]
#> [1,]    1    0
#> [2,]    2    3

na_replace(c(1, 2, NA, NA))
#> [1] 1 2 0 0
na_replace(c(1, 2, NA, NA), replacement = -1)
#> [1]  1  2 -1 -1
na_replace(c(1, 2, NA, NA), replacement = c(0, -1))
#> [1]  1  2  0 -1

na_replace(c(Sys.Date(), NA)) # replacement defaults to 'today'
#> [1] "2022-10-30" "2022-10-30"

na_replace(c(TRUE, FALSE, NA))
#> [1]  TRUE FALSE FALSE
na_replace(c(TRUE, FALSE, NA), replacement = TRUE)
#> [1]  TRUE FALSE  TRUE

# we're flexible, the class only remains the same if
# the replacement value allows it
na_replace(c(1, 2, 3, NA), replacement = "-")
#> [1] "1" "2" "3" "-"

# data.frame is a special case
mtcars[1:6, c("mpg", "hp")] <- NA
head(mtcars)
#>                   mpg cyl disp hp drat    wt  qsec vs am gear carb
#> Mazda RX4          NA   6  160 NA 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag      NA   6  160 NA 3.90 2.875 17.02  0  1    4    4
#> Datsun 710         NA   4  108 NA 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive     NA   6  258 NA 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout  NA   8  360 NA 3.15 3.440 17.02  0  0    3    2
#> Valiant            NA   6  225 NA 2.76 3.460 20.22  1  0    3    1
head(na_replace(mtcars, mpg, hp)) # no need to quote columns (but you can)
#>                   mpg cyl disp hp drat    wt  qsec vs am gear carb
#> Mazda RX4           0   6  160  0 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag       0   6  160  0 3.90 2.875 17.02  0  1    4    4
#> Datsun 710          0   4  108  0 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive      0   6  258  0 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout   0   8  360  0 3.15 3.440 17.02  0  0    3    2
#> Valiant             0   6  225  0 2.76 3.460 20.22  1  0    3    1
head(na_replace(mtcars, mpg, hp, replacement = c(999, 123)))
#>                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         999   6  160 123 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     999   6  160 123 3.90 2.875 17.02  0  1    4    4
#> Datsun 710        999   4  108 123 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    999   6  258 123 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 999   8  360 123 3.15 3.440 17.02  0  0    3    2
#> Valiant           999   6  225 123 2.76 3.460 20.22  1  0    3    1

if (FALSE) {
# practical way using tidyverse
library(dplyr)
starwars %>% 
  na_replace()

# even maintains groups
starwars %>%
  group_by(hair_color) %>%
  na_replace(hair_color, replacement = "TEST!") %>% 
  summarise(n = n())
}