R Basic

Efficiently Check If a Vector is Empty in R

The most efficient and idiomatic way to check if a vector is empty in R is by using the built-in length() function and checking whether its length is 0. If “length(vec) == 0” returns TRUE, the vector is empty; otherwise, it is not empty.

An empty vector means it has no elements. One thing to note is that even if a vector is empty, it can still have a data type. For example, you can create an empty numeric vector using the numeric() function or the character() function to create a character vector.

# Defining an empty vector
empty_vector <- c()

# Checking if the vector is empty
is_empty <- length(empty_vector) == 0

# Printing the result
print(is_empty)

# Output
# TRUE

Since the vector is empty, we got TRUE as output.

Let’s check out for a non-empty vector:

# Defining a non-empty vector
non_empty_vector <- c(1, 2, 3)

# Checking if the vector is empty
is_empty <- length(non_empty_vector) == 0

# Printing the result
print(is_empty)


# Output
# FALSE

Checking with a conditional statement

The length(vec) == 0 returns a boolean result, which is why we can use this type of code snippet in the if-else statement to process further operations.

# Defining a non-empty vector
non_empty_vector <- c("KB", "KL")

# Checking if the vector is empty using a conditional statement
if (length(non_empty_vector) == 0) {
  print("The vector is empty!")
} else {
  print("The vector is not empty")
}

Output

The vector is not empty

If you check for an empty vector, it will return the below output.

[1] The vector is empty!

Why should we check?

If you want to write robust, reliable, and error-free code that will prevent any future unexpected errors, you should always check whether your input object or vector is empty. It is considered best practice to include all types of unforeseen scenarios.

Difference between NULL vector and empty vector

An empty vector is not the same as a NULL vector.

The main difference is that the NULL vector itself represents the absence of an object, whereas an empty vector just does not contain any element, but it is still an R object.

# Defining empty vector
empty_vec <- c()
is.null(empty_vec)

# Output: FALSE


# Defining NULL object
null_object <- NULL
is.null(null_object)

# Output: TRUE

The is.null() function returns TRUE for a NULL object, not an empty object.

Shorter way to check

You can also use the shorter syntax “!length(vec)”.

# Defining empty vector
empty_vec <- c()

# Checking if a Vector is empty using shorter syntax
if (!length(empty_vec)) {
  print("It is empty")
} else {
  print("It is not empty")
}

# Defining a non-empty vector
non_empty_vector <- c(19, 21)

# Checking if It is empty using shorter syntax
if (!length(non_empty_vector)) {
  print("It is empty")
} else {
  print("It is not empty")
}

Output

[1] "It is empty"
[1] "It is not empty"

That’s all

Recent Posts

How to Check If File and Folder Already Exists in R

Whether you are reading or writing files via programs in the file system, it is…

2 days ago

How to Check Data type of a Variable in R

When it comes to checking the data type of a variable, it depends on what…

3 days ago

Mastering grepl() Function in R

The grepl() function (stands for "grep logical") in R searches for patterns within each element…

4 days ago

zip(), unzip() and tar(), untar() Functions in R

The zip() function creates a new zip archive file. You must ensure that the zip tool…

5 days ago

How to Create Directory and File If It doesn’t Exist in R

When working with file systems, checking the directory or file existence is always better before…

6 days ago

How to Create a Grouped Boxplot in R

To create a grouped boxplot in R, we can use the ggplot2 library's aes() and…

1 week ago