# Check if Number is Integer in R (3 Examples)

In this R tutorial you’ll learn how to **test whether a number is an integer** (i.e. a whole number).

Table of contents:

- Example Data
- Example 1: Check if Number is Integer with round Function
- Example 2: Check if Number is Integer with %% Operator
- Example 3: Check if Number is Integer with all.equal Function
- Video, Further Resources & Summary

Let’s get started.

## Example Data

First, let’s create a data object called x, which is containing an integer value (i.e. 5):

x <- 5 # Create integer object |

x <- 5 # Create integer object

Now you might say: Easy going, I’m simply using the is.integer function:

is.integer(x) # Apply is.integer function # FALSE |

is.integer(x) # Apply is.integer function # FALSE

Be careful! As you can see, the is.integer function is returning the logical value FALSE to the RStudio console, even though our value x is obviously an integer.

The reason for that can be found in the R help documentation of the is.integer function:

is.integer(x) does not test if x contains integer numbers! For that, use round, as in the function is.wholenumber(x) in the examples.

The reason for that is that the is.integer function is checking whether a data object has the **integer class** (or data type). Since our data object has the numeric data class, the is.integer function returns FALSE.

In the following examples, I’ll show how to check for whole numbers properly.

## Example 1: Check if Number is Integer with round Function

Example 1 tests for integer numbers using the round function:

x == round(x) # Check for integer with round function # TRUE |

x == round(x) # Check for integer with round function # TRUE

The RStudio console returns the logical value TRUE, i.e. our data object x is an integer.

## Example 2: Check if Number is Integer with %% Operator

We can also use the %% operator to check if our number is an integer:

x %% 1 == 0 # Check for integer with %% operator # TRUE |

x %% 1 == 0 # Check for integer with %% operator # TRUE

## Example 3: Check if Number is Integer with all.equal Function

The third solution I want to show you is based on the all.equal function. Again, the RStudio console output is TRUE:

all.equal(x, as.integer(x)) # Check for integer with all.equal function # TRUE |

all.equal(x, as.integer(x)) # Check for integer with all.equal function # TRUE

## Video, Further Resources & Summary

I have recently published a video tutorial on my YouTube channel, which explains the R programming codes of this article. You can find the video below.

**Please accept YouTube cookies to play this video.** By accepting you will be accessing content from YouTube, a service provided by an external third party.

If you accept this notice, your choice will be saved and the page will refresh.

Furthermore, you could have a look at the related articles that I have published on this website. You can find a selection of posts here.

This article illustrated how to **test if a value or a vector of values contains whole numbers** in the R programming language. Let me know in the comments below, if you have additional questions.

**5**/

**5**(

**1**vote )

### Statistics Globe Newsletter