0

I used to clean my dataframe by defining NA values as such:

my_df%>%
dplyr::mutate_all(funs(na_if(., "N/A") ))

Basically, I want to set certain values to NA across my entire dataframe. It worked well until the latest package update. It fires the following error: Caused by error in `na_if()`: ! Can't convert `y` <character> to match type of `x` <double>

There is a related question, but the answer is not working on the entire data frame in one go.

MsGISRocker
  • 588
  • 4
  • 21
  • 1
    You most likely want to mutate only character columns and not the entire dataframe, not sure how `dplyr` handled this before, but the included error is pretty much what I'd expect when forcing such operation on some numeric variable. To adapt the linked answer, try with `my_df %>% mutate(across(where(is.character), ~ na_if(.x, "N/A")))` .Though in most cases you can just avoid that situation by defining `NA` strings during the import with something like `read.csv(..., na.strings = "N/A",)` or `readr::read_csv(..., na = c("", "NA", "N/A"))` – margusl Jun 25 '23 at 10:19
  • 2
    Well, readr::read_csv(... , na = c("", "NA", "N/A")) is a good one. – MsGISRocker Jun 26 '23 at 13:32

0 Answers0