Hi Community, Im want to compare two columns but contains NA
values. Im use this code but the comparison don run well. How fix better this?
Why the both if_else()
show different results?
library(tidyverse)
example <-structure(list(SUBSP = c(NA, NA, NA, "TENUIFLORUS", "TENUIFLORUS",
NA, NA, NA, NA), `NEW-SUBTAXA` = c("0", "0", "0", "0", "0", "0",
"0", "var. marginata", "var. leucacrantha"), NEW_SUBTAXA = c(NA,
NA, NA, "TENUIFLORUS", "TENUIFLORUS", NA, NA, "var. marginata",
"var. leucacrantha")), row.names = c(2284L, 2339L, 3118L, 3460L,
9571L, 9837L, 9940L, 5028L, 8839L), class = "data.frame")
example <- example |>
dplyr::mutate(NEW_SUBTAXA_CHECK = dplyr::if_else(identical(SUBSP, NEW_SUBTAXA)| (is.na(SUBSP) & is.na(NEW_SUBTAXA)),'IGUAL','DIFERENTE')) |>
dplyr::mutate(NEW_SUBTAXA_CHECK2 = dplyr::if_else(SUBSP==NEW_SUBTAXA,'IGUAL','DIFERENTE'))
example
#> SUBSP NEW-SUBTAXA NEW_SUBTAXA NEW_SUBTAXA_CHECK
#> 2284 <NA> 0 <NA> IGUAL
#> 2339 <NA> 0 <NA> IGUAL
#> 3118 <NA> 0 <NA> IGUAL
#> 3460 TENUIFLORUS 0 TENUIFLORUS DIFERENTE
#> 9571 TENUIFLORUS 0 TENUIFLORUS DIFERENTE
#> 9837 <NA> 0 <NA> IGUAL
#> 9940 <NA> 0 <NA> IGUAL
#> 5028 <NA> var. marginata var. marginata DIFERENTE
#> 8839 <NA> var. leucacrantha var. leucacrantha DIFERENTE
#> NEW_SUBTAXA_CHECK2
#> 2284 <NA>
#> 2339 <NA>
#> 3118 <NA>
#> 3460 IGUAL
#> 9571 IGUAL
#> 9837 <NA>
#> 9940 <NA>
#> 5028 <NA>
#> 8839 <NA>