Dataset cleaning

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.

3 Likes

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"))
4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.