Hi,
I want to clean my dataset, putting "0" where I have "NA" value. My code is:
library(dplyr)
data <- starwars
data_clean <- data[is.na(data)] <- 0
This don't work because I have "na" values in both integer and character columns. Is there a way to clean my dataset?
Something like this will change numeric NAs to 0.
library(dplyr)
data <- starwars
data_clean <- mutate(data, across(where(is.numeric), tidyr::replace_na, 0))
We are mutating across all numeric columns. To each we apply "tidyr::replace-na", which replaces NA with something. In this case we replace with 0.
That's great!
Is there a way to replace "NA" in character column?
Sure thing, just add another across!
library(dplyr)
data <- starwars
data_clean <- mutate(data,
across(where(is.numeric), tidyr::replace_na, 0),
across(where(is.character), tidyr::replace_na, "0"))