change empty cells in all factor columns to "Missing"

I have a data frame of around 100 columns. I want to find a way to change empty cells to a "Missing" value in all columns that are factors. After extensive search, I managed to find a few solutions online

mutate_if(is.factor, fct_explicit_na, na_level = "Missing")
 mutate_if(is.factor, ~replace_na(., "Missing"))
 mutate(across(where(is.factor), ~fct_explicit_na(., na_level = "Missing"))

none of which unfortunately work.
The whole code i am using

TempData <- TempData %>%
              transform(
                         A   = as.factor(A)
                        ,B   = as.factor(B)
                        ,C   = as.factor(C)
                        ,D   = as.factor(D)) %>%
              mutate_if(is.factor, fct_explicit_na, na_level = "Missing")

In what other way can I replace empty values to "Missing" in a way that I dont have to type every single column.

Thanks

The code you shared examples of woukd be expected to work if your assumptions hold true thay the columns to be processed are factors and that the empty cells are NA s, because you say its not working for you, experience wouldnsuggest your empty cells are not NA but possibly an empty string; if so you could use ```
fct_recode()

To change that.

If thats not your issue, the next step would be to reprex.
1 Like

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.