Michael
Michael

Reputation: 345

Cannot replace NA with 0 for a factor in R

I am trying to replace <NA> values in a factor column with 0. However, when I run the following:

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

I get this error:

Error: Assigned data `value` must be compatible with existing data.
i Error occurred for column `CODE`.
x Can't convert <double> to <factor<5cb9e>>.
Run `rlang::last_error()` to see where the error occurred.

The column labelled 'CODE' is a factor. I previously had no issue with this but I have just updated all of my packages and my version of RStudio to all of the latest versions and now this error occurs.

Is this way no longer accurate?

UPDATE:

I have managed to solve this using the following:

pastP$CODE <- as.character(pastP$CODE)
pastP$CODE[is.na(pastP$CODE)] <-0
pastP$CODE <- factor(pastP$CODE)

Although still unsure why the first error occurred.

Upvotes: 0

Views: 739

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388807

Factors needed to be treated differently. If you have a dataframe include additional levels in your data before changing the values.

pastP <- data.frame(CODE = factor(c(3, 4, NA, 1)))
levels(pastP$CODE) <- c(levels(pastP$CODE), 0)
pastP[is.na(pastP)] <- 0

If you have a tibble you can use fct_explicit_na -

library(dplyr)
library(forcats)

pastP <- tibble(CODE = factor(c(3, 4, NA, 1)))

pastP %>% mutate(CODE = fct_explicit_na(CODE, '0'))

#  CODE 
#  <fct>
#1 3    
#2 4    
#3 0    
#4 1    

Upvotes: 3

Related Questions