I want to run switch loop and be able to match NA
, for example:
switch(var, match1 = do something, match3 = do something)
How can do it if var
is NA
like so:
switch(var, match1 = do something, match3 = do something, NA = do something)
I've also tried is.na()
instead of NA
and it didn't work.
In this situation, NA has to be escaped using backticks (or quotes)
switch(var, match1 = do something, `NA` = do something)
One thing to note is that you cannot switch
NA values directly. For example
switch(NA, `NA` = 1)
does not work, and you should use e.g.
switch(as.character(NA), `NA` = 1)
# [1] 1
instead. It is probably better to use var[is.na(var)] <- ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With