In Corresponding to NA in the switch () loop , the responder indicated that using `NA`
in switch
would correspond to missing values. However, "NA"
also consistent. I have a character vector that contains both a string "NA"
and missing values NA
. I pass the elements of this vector one by one before switch
, but cannot distinguish between two
for (k in c(NA, "NA")) {
cat(switch(k, "NA_character_" = "C", "NA" = "B", `NA` = "A"))
}
#> BB
for (k in c(NA, "NA")) {
cat(switch(k, "NA_character_" = "C", `NA` = "A", "NA" = "B"))
}
#> AA
I know what I could use if (is.na(k))
to distinguish them, but the purpose of use switch
was to restrict nested statements if ... else
, so I would prefer to use it only switch
if it is just a matter of choosing the right name in the list of alternatives. I note that the help file says (with my accent):
If EXPR evaluates a character string, then that string matches (exactly) the names of the elements in ...
therefore, I wonder if there is a specific “exactly equal NA_character_
” value that is applicable here.
source
share