39

In a script I try to get running sometimes are variables being filled with '' (which means: completely empty), e.g.

variable <- ''

Does anyone know of a method to check if variable has the value ''?

is.null(variable) doesn't seem to work. '' is not the same as NULL.

3 Answers 3

41

'' is an empty character. It does not mean “completely empty” – that is indeed NULL.

To test for it, just check for equality:

if (variable == '') …

However, the error you’re getting,

missing value where TRUE/FALSE needed

means that there’s a missing value in your variable, i.e. NA. if cannot deal with missing values. An NA occurs as a result of many computations which themselves contain an NA value. For instance, comparing NA to any value (even NA itself) again yields NA:

variable = NA
variable == NA
# [1] NA

Since if expects TRUE or FALSE, it cannot deal with NA. If there’s a chance that your values can be NA, you need to check for this explicitly:

if (is.na(variable) || variable == '') …

However, it’s normally a better idea to exclude NA values from your data from the get-go, so that they shouldn’t propagate into a situation like the above.

9
  • Thanks! perhaps I asked the wrong question. this is the error i get: "Error in if (variable == "") { : missing value where TRUE/FALSE needed". Variable doesn't even seem to exist (must be an error in my script). Is there a way of checking if a variable exists? Commented Jul 15, 2013 at 13:09
  • @user1983395 Indeed, that’s a completely different question. Let me amend my answer. Commented Jul 15, 2013 at 13:11
  • 2
    An old thread, but I see that the response given to the specific error here is not correct. The "missing value where TRUE/FALSE needed" error does not mean that the variable doesn't exist, but instead that it is set to NA (the missing value code). If it didn't exist, the error would have been "object 'variable' not found." R complains because the logic is undefined; NA is neither equal to nor not equal to anything because it signifies that the value is missing and so could be either. Use is.na() and the boolean operators && or || to define what it should do when a missing is found. Commented Feb 10, 2015 at 2:06
  • @Aaron Correct, thanks for pointing this out. I hadn’t actually paid attention to the specific error description. Commented Feb 10, 2015 at 10:00
  • 1
    @PatrickT Very (very!) few exceptions aside, the type of a variable in a given piece should be well-known and fixed. R is unfortunately unhelpful here, but it’s the programmer’s job to keep track of that information when writing code. There’s no general solution to deal with arbitrary types, the code by necessity needs to make assumptions, and it’s totally OK if code fails if the user violates these assumptions (this is known as fail-fast). Commented Dec 31, 2021 at 11:57
3

In stringi package there is function for this.

require(stringi)    
stri_isempty(c("A","")) 

You can also install this package from github: https://github.com/Rexamine/stringi

1

If you wish to both check and replace these values with NAs all at once, as is usually the case, just use dplyr::na_if():

variable <- ''
dplyr::na_if(variable, "")
#> [1] NA

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.