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!
>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