replacing <NA> within factor variables as 0

I have a dataset with both character and numeric variables - I am trying to replace all NA's and empty values in this data with "0". For a continuous variable, the NA/empty value should be replaced with a "numeric 0". For factor variables, the NA/empty value should be replaced with a "factor 0".

In the past, I used to use a standard command for replacing all NA's with 0 (in the below code, "df" represents the data frame containing the data):

df[df == NA] <- 0

I tried the above code on my data, but I still noticed that within the factor variables, this code was not able to replace <NA> values with 0. <NA> 's are still present.

I tried several approaches:

1st Approach:

df[is.na(df)] <- 0

But this did not work:

Warning message: 
In '[<-.factor'('*tmp*',thisvar, value = 0):
invalid factor level, NA generated

Second Approach : I tried for one of the factor variables

library(car)
df$some_factor_var <- recode(df$some_factor_var, "NA = 0")

But this replaced every value within "some_factor_var" as 0

Third Approach : I tried again for one of the factor variables

library(forcats)
fct_explicit_na(df$some_factor_var,0)

Error: Can't convert a double vector to a character vector

Can someone please show me how to fix this problem? Is there a way to replace ALL empty/missing/NA values for all variables at once?

Thanks

a factor is an integer type value with character labels. I assume that you want the label corresponding to NA value to be '0' so I think you should provide it to fct_explicit_na as '0' rather than 0.

I think you could further try code like :

hiris[1,] <- rep(NA,5)
hiris
library(tidyverse)
mutate(hiris,
       across(where(is.numeric),~replace_na(.,0)),
       across(where(is.factor),~fct_explicit_na(.,'0')))

package sjmisc has a useful version of replace_na

sjmisc::replace_na(hiris,value = 0)
1 Like

thank you! I will try this!

This topic was automatically closed 21 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.