# Test if Vector is Empty in R (Example)

This article shows how to **check if a vector object is empty** in the R programming language.

The tutorial contains these contents:

Let’s dive right into the example…

## Creation of Example Data

First and foremost, I have to define some data that we can use in the following examples:

vec1 <- vector() # Create empty vector vec1 # Print empty vector # logical(0) |

vec1 <- vector() # Create empty vector vec1 # Print empty vector # logical(0)

vec2 <- 1:5 # Create non-empty vector vec2 # Print non-empty vector # [1] 1 2 3 4 5 |

vec2 <- 1:5 # Create non-empty vector vec2 # Print non-empty vector # [1] 1 2 3 4 5

The previous R syntax has created two vector objects called vec1 and vec2. You may already notice that the first vector is empty, but the second vector contains several elements.

However, let’s check this more systematically!

## Example: Test if Vector Object is Empty Using length() Function

This example explains how to check if a vector object is empty.

For this task, we can apply the length function as shown below:

length(vec1) # Check length of vector # [1] 0 |

length(vec1) # Check length of vector # [1] 0

As you can see, the length function has returned the value 0 after applying it to our first vector object (i.e. vec1 is empty).

We may also use the length function in combination with the == to return a logical indicator whether our data object is empty:

length(vec1) == 0 # Logical whether vector is empty # [1] TRUE |

length(vec1) == 0 # Logical whether vector is empty # [1] TRUE

The R code above has returned the logical indicator TRUE, i.e. our data is empty.

For comparison, let’s apply the same syntax to our second example vector:

length(vec2) # Check length of vector # [1] 5 |

length(vec2) # Check length of vector # [1] 5

The length function has returned the value 5, i.e. our second vector object is not empty.

This gets confirmed by running the following R code:

length(vec2) == 0 # Logical whether vector is empty # [1] FALSE |

length(vec2) == 0 # Logical whether vector is empty # [1] FALSE

The RStudio console has returned the logical value FALSE, i.e. our second vector is not empty.

## Video & Further Resources

Do you want to learn more about the evaluation of whether a vector object is empty? Then you might want to have a look at the following video on my YouTube channel. I’m explaining the R codes of this article in the video:

**Please accept YouTube cookies to play this video.** By accepting you will be accessing content from YouTube, a service provided by an external third party.

If you accept this notice, your choice will be saved and the page will refresh.

In addition, you might read some of the related tutorials on my website.

- Test if Vector Contains Given Element in R
- Test for Equality of All Vector Elements
- Test if Character is in String
- interactive() Function in R
- R Programming Overview

In summary: In this article, I have shown how to **test whether a vector object is empty** in the R programming language. Kindly let me know in the comments, in case you have further questions.

### Statistics Globe Newsletter