Handling Errors in R: Argument is of Length Zero (2 Examples)

On this page, I’ll illustrate how to handle the error message “argument is of length zero” in the R programming language.

Example 1: Replicating the Error Message: Argument is of Length Zero

foo <- character()        # Empty character object
if(foo == "X") {          # Empty character object in if-statement
  foo
}
# Error in if (foo == "X") { : argument is of length zero

Example 2: Solving the Error Message: Argument is of Length Zero

bar <- "X"                # Create character object with one value
if(bar == "X") {          # Non-empty character object in if-statement
  bar
}
# "X"

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top