# How to Catch an integer(0) in R (2 Examples)

In this R post youâ€™ll learn how to identify an integer of length zero.

The page contains the following topics:

So letâ€™s just jump right in!

## Example Data

Consider the exemplifying data below.

```x <- which("a" == "b")             # Create integer with length zero
x                                  # Print example integer
# integer(0)```

As you can see based on the previous output of the RStudio console, our example data object is an integer with a length of zero.

## Example 1: Test for Length of Data Object Using length() Function

This example shows how to check for the length of a data object to identify whether an integer has a length of zero.

For this, we simply need to apply the length function to our data object:

```length(x)                          # Apply length function
# [1] 0```

The RStudio console returns the value 0, i.e. our data object is an integer(0) object.

## Example 2: Create User-Defined Function to Test for Length of Data Object

In this example, Iâ€™ll explain how to create a user defined function that returns information on the length of a data object.

First, we have to create our function:

```fun_int0 <- function(my_data) {    # Create user-defined function

if(length(my_data) == 0 & is.integer(my_data)) {

print("The data object is integer(0).")
# [1] "The data object is integer(0)."

} else {

print("The data object is NOT integer(0).")
# [1] "The data object is NOT integer(0)."
}
}```

Now, we can apply our manually defined function to our example data:

```fun_int0(x)                        # Apply user-defined function to integer(0)
# [1] "The data object is integer(0)."```

The RStudio console shows that our data is an integer(0) object.

Letâ€™s apply our function to a normal integer vector with longer length:

```fun_int0(1:5)                      # Apply user-defined function to longer object
# [1] "The data object is NOT integer(0)."```

The RStudio console shows that the vector 1:5 is not an integer(0) data object.

## Video, Further Resources & Summary

Would you like to know more about integers and other data types? Then you may watch the following video of my YouTube channel. I explain the R programming codes of this article in the video tutorial:

To summarize: In this R tutorial you have learned how to catch an empty integer.

Please note that a similar syntax as shown in the present tutorial could be used to test for other vectors of length 0 such as character(0), numeric(0), or factor(0).

If you have any further questions, donâ€™t hesitate to let me know in the comments.

Subscribe to the Statistics Globe Newsletter