Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I change 0 values in numeric data type columns to NA, without changing FALSE operators in logical data type columns to NA?

I want to convert all 0 numerical values in a spreadsheet to NA. The code below changes all of my 0 values in numerical columns to NA as intended, however, FALSE values in logical columns are also being changed to NA.

dataframe <- na_if(dataframe, 0)

Is there a way around this that doesn’t require me splitting the data frame up into logical and numeric parts, converting 0s to NAs on the numeric data, then merging it? Thank you!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We may have to loop across numeric columns because TRUE/FALSE are otherwise 1/0 when coerced

library(dplyr)
FibreDatabase <- FibreDatabase %>%
    mutate(across(where(is.numeric), na_if, 0))

If we check the source code of na_if

...
 x[x == y] <- NA
...

which does the conversion

> df1 <- data.frame(v1 = FALSE, v2 = c(0, 1))
> df1 == 0
       v1    v2
[1,] TRUE  TRUE
[2,] TRUE FALSE

Here, the FALSE are also coerced to 0 and it returns TRUE when we do the == as below

> df1 %>%
    mutate(across(where(is.numeric), na_if, 0))
     v1 v2
1 FALSE NA
2 FALSE  1
> na_if(df1, 0)
  v1 v2
1 NA NA
2 NA  1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading