'' is an empty character. This does not mean "completely empty" - it is really NULL .
To verify this, simply check for equality:
if (variable == '') β¦
If you want to check if a variable exists, you need to use ... exists :
if (exists('variable')) β¦
But in fact, in normal code there are very few cases of using exists , since, as the author of the code, you should know which variables exist and which do not. Rather, it is primarily useful in library functions.
However, the error you get is
missing value requiring TRUE / FALSE
does not mean that the variable does not exist. Rather, if cannot deal with missing values ββ- i.e. NA . NA is the result of many calculations that themselves contain the value NA . For example, comparing NA with any value (even NA ) again gives NA :
variable = NA variable == NA
Since if expects TRUE or FALSE , it cannot deal with NA . If you have a chance that your values ββmay be NA , you need to check this explicitly:
if (is.na(variable) || variable == '') β¦
However, as a rule, it is best to exclude NA values ββfrom your data from get-go so that they do not propagate in such a situation, as indicated above.
Konrad Rudolph
source share