# R Error: Argument is of Length Zero (Example)

In this article, I’ll illustrate how to **fix the error message “argument is of length zero”** in the R programming language.

Table of contents:

So now the part you have been waiting for – the examples…

## Example 1: Reproduce the Error: argument is of length zero

Example 1 shows how to replicate the error “argument is of length zero” when using if-statements in R.

Consider the following empty data object:

x1 <- numeric() # Create empty data object x1 # Print empty data object # numeric(0)

Now, let’s assume that we want to use this data object within the if() function:

if(x1 > 5) { # Using empty data object in if-statement x1 } # Error in if (x > 5) { : argument is of length zero

The RStudio console returns the error message “argument is of length zero”.

The reason for this is that we have used an empty data object in the logical condition within the if-statement (i.e. x1 > 5). This is not possible in R and therefore the RStudio console returns an error.

Next, I’ll explain how to solve this problem…

## Example 2: Fix the Error: argument is of length zero

This example explains how to deal with the error message “argument is of length zero” in R. For this, we have to create a vector object that does not have a length of zero:

x2 <- 10 # Create numeric data object x2 # Print data object # 10

Now, let’s use this data object for the logical condition of our if-statement:

if(x2 > 5) { # Using numeric data object in if-statement x2 } # 10

Works fine!

## Video & Further Resources

Do you want to learn more about errors in R programming? Then you may have a look at the following video of my YouTube channel. I’m explaining the examples of this article in the video:

Furthermore, you might want to have a look at the related articles of my website. Some tutorials about related topics such as data objects and vectors can be found here.

- How to Create a Vector of Zero Length
- Check if Object is Defined (exists in R)
- Fixing Error & Warning Messages in R (Overview)
- R Programming Tutorials

This tutorial showed how to **handle the error message “argument is of length zero”** in R. In case you have any additional comments or questions, please let me know in the comments section. Furthermore, don’t forget to subscribe to my email newsletter in order to receive regular updates on new tutorials.