This provides a convenient way to recode "NA" as another value for instance "NaN", "n/a" or any other value a user wishes to use.
recode_na_as(
df,
value = 0,
subset_cols = NULL,
pattern_type = NULL,
pattern = NULL,
case_sensitive = FALSE,
...
)
A data.frame object for which recoding is to be done.
The value to convert to `NA`. We can for instance change "n/a" to `NA` or any other value.
An optional character vector to define columns for which changes are required.
One of contains', 'starts_with' or 'ends_with'.
A character pattern to match
Defaults to FALSE. Patterns are case insensitive if TRUE
Other arguments to other functions
An object of the same type as x with NAs replaced with the desired value.
head(recode_na_as(airquality, "n/a"))
#> Ozone Solar.R Wind Temp Month Day
#> 1 41 190 7.4 67 5 1
#> 2 36 118 8.0 72 5 2
#> 3 12 149 12.6 74 5 3
#> 4 18 313 11.5 62 5 4
#> 5 n/a n/a 14.3 56 5 5
#> 6 28 n/a 14.9 66 5 6
head(recode_na_as(airquality, subset_cols = "Ozone", value = "N/A"))
#> Ozone Solar.R Wind Temp Month Day
#> 1 41 190 7.4 67 5 1
#> 2 36 118 8.0 72 5 2
#> 3 12 149 12.6 74 5 3
#> 4 18 313 11.5 62 5 4
#> 5 N/A NA 14.3 56 5 5
#> 6 28 NA 14.9 66 5 6
head(recode_na_as(airquality, value=0, pattern_type="starts_with",
pattern="Solar"))
#> Ozone Solar.R Wind Temp Month Day
#> 1 41 190 7.4 67 5 1
#> 2 36 118 8.0 72 5 2
#> 3 12 149 12.6 74 5 3
#> 4 18 313 11.5 62 5 4
#> 5 NA 0 14.3 56 5 5
#> 6 28 0 14.9 66 5 6