How to Solve R Error: argument is of length zero

by | Programming, R, Tips

This error occurs if you try to perform a logical comparison in an if-statement with a zero-length variable. This error can happen if you define a variable with zero length or if you attempt to access a column of a data frame that does not exist.

You can solve the error by assigning a non-zero value to a variable or accessing a column that exists in a data frame.

This tutorial will go through the error in detail and how to solve it with code examples.


Example #1: Vector of Length Zero

Let’s look at an example of using a vector of length zero in an if statement.

First, we will create a numeric variable with a length of zero using the double() function which is identical to the numeric() function.

num <- double()

Next, we will check if the numeric variable is less than 7 in an if statement. If the if statement evaluates to TRUE then the R interpreter prints the number.

if (num < 7) {

   num

}

Let’s run the code to see what happens:

Error in if (num < 7) { : argument is of length zero

The error occurs because the variable num has a length of zero:

print(length(num))
0

Solution

We can solve the error by using a numeric variable with a non-zero value. Let’s look at the revised code:

vec <- 5

if (vec < 7){

    vec

}

Let’s run the code to get the result:

5

We can check if a value is of length zero using the isTRUE function.

isTRUE(x) is equivalent to:

is.logical(x) && length(x) == 1 && !is.na(x) && x}

The use of isTRUE in an if statement, for example if(isTRUE(condition)) may be preferable to if(cond) as the if statement with isTRUE evaluates to FALSE for zero-length variables or NAs.

num <- numeric()
isTRUE(num)
FALSE

Let’s look at the revised code with the if-statement.

num <- numeric()

if (isTRUE(num) && num < 7){

    num

}

If we run the code the R interpreter will not print the value of num.

Example #2: Non-existent Data frame Column

We can also encounter this error by trying to evaluate a condition based on a non-existent data frame column.

Let’s look at an example of a data frame with three columns.

X <- c(23, 41, 55, 32, 26, 78)

Y <- c(10, 11, 100, 44, 22, 9)

Z <- c(61, 99, 55, 42, 36, 1)

df <- data.frame(X, Y, Z)

Next, we will define a for loop to iterate of the rows in the data frame and compare the numeric values between two of the columns in an if statement. If the numeric values for the two columns are equal then we print the value to the console.

for (i in 1:nrow(df))

if(df[i,"X"] == df[i, "A"]) {

    df[i, "X"]

}

Let’s run the code to see what happens:

Error in if (df[i, "X"] == df[i, "A"]) { : argument is of length zero

The error occurs because the column “A” does not exist. The length of a non-existent column is zero.

Solution

We can solve the error by using a column name that exists. We can get the column names of a data frame using the colnames() function.

colnames(df)
"X" "Y" "Z"

Let’s look at the revised code:

X <- c(23, 41, 55, 32, 26, 78)

Y <- c(10, 11, 100, 44, 22, 9)

Z <- c(61, 99, 55, 42, 36, 1)

df <- data.frame(X, Y, Z)

for (i in 1:nrow(df))

if(df[i,"X"] == df[i, "Z"]) {

    print(df[i, "X"])

}

We changed the if-statement to compare values in column “X” and “Z“. Let’s run the code to find the value that exists in both columns:

55

Summary

Congratulations on reading to the end of this tutorial!

For further reading on R-related errors, go to the articles: 

For more on using colnames in R, go to the article:

Go to the online courses page on R to learn more about coding in R for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨