Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if variable has the value ''

Tags:

string

r

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.

like image 850
rdatasculptor Avatar asked Jul 15 '13 13:07

rdatasculptor


People also ask

How do you check if a variable has a value in C++?

Unlike C# and some other languages, variables in C++ will always have some data contained in them. It's just that when you fail to initialize them yourself, that data is unpredictable garbage. Therefore there is no way to check whether or not a variable has been assigned.

How do you check if a variable has a specific value in Python?

To check if a local variable exists in Python, use the in operator against the output of locals() function, which has a dictionary of a current local variables table. The “in operator” returns a boolean value. If a variable exists, it returns True otherwise False.

Which function is used to check whether a variable have value?

The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.

How do you check if a variable is empty in JS?

The typeof operator for undefined value returns undefined . Hence, you can check the undefined value using typeof operator. Also, null values are checked using the === operator.

How can I test whether a variable has a value in JavaScript?

How can I test whether a variable has a value in JavaScript? Use multiple conditional operators in the checkSign function to check if a number is positive, negative or zero. The function should return "positive", "negative" or "zero". check whether an object is a function?

How to check whether a variable contains a value or not?

The first method we will use for checking a variable is the if...else statement. We can also check whether a variable contains a value or not by using this method. In the example above, through the line if [ x"$ {VAR}" == "x" ];, we checked whether the variable contains a value. When you execute the code, you will get the following output:

How do I test if a variable is available?

Get to testing with the Get-Variable, PS drive and the -eq operator. Variables are just about the most ubiquitous element inside any PowerShell script. Variables can easily be created and referenced inside your script. But sometimes you just need to test to see if they are available or not.

What does this condition check for in a variable?

This condition checks overall if the mentioned variable has any value. It’s not checking the specific value. do you suggest what I do if we needs to check this type of value? If you have this (Say Var2 contains any one of these values ->3-133439413,3-133439424 etc) values in variable, then compare as suggested in previous replies.


2 Answers

'' 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.

like image 91
Konrad Rudolph Avatar answered Sep 29 '22 10:09

Konrad Rudolph


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

like image 37
bartektartanus Avatar answered Sep 29 '22 11:09

bartektartanus