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:

 

 

In addition, you could read the other tutorials on this homepage.

 

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

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


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.

Top