R dplyr to replace all empty factors with NA

Instead of writing and reading data to fill in all the empty factors in this method,

na.strings=c("","NA") 

I just wanted to apply the function to all columns and substitute voids with NA. I have chosen the factor columns so far, but I don’t know what to do next.

 df %>% select_if(is.factor) %>% .... 

How can I do this, preferably using the dplyr and / or apply methods

+5
source share
1 answer

We can use mutate_if

 df <- df %>% mutate_if(is.factor, funs(factor(replace(., .=="", NA)))) 

Or using base R , we can assign specific levels NA

 j1 <- sapply(df, is.factor) df[j1] <- lapply(df[j1], function(x) {is.na(x) <- levels(x)==""; x}) 

data

 df <- data.frame(col1 = c("", "A", "B", ""), col2 = c("A", "", "", "C"), col3 = 1:4) 
+5
source

Source: https://habr.com/ru/post/1265989/


All Articles