R Basic

is.integer() Function: Checking Integer Values in R

R is.integer() function checks if a variable or an object is of type integer. It returns TRUE if it is an integer and FALSE otherwise. Numbers are stored in either integer or double (floating-point) data types.

In R, the simple number 5 is not of type integer; it is a value of type double. To create an integer, you have to append “L” after each numeric value. Let’s verify this statement.

typeof(5) # [1] "double"

typeof(5L) # [1] "integer"

We can use the “typeof()” function to check the internal storage type of the object. In our case, 5 is stored as a double, and 5L is stored as an integer.

Integer generally uses less memory than double.

Now that we understand the distinction between integer and double, let’s use our main function is.integer() on these values:

is.integer(5) # [1] FALSE

is.integer(5L) # [1] TRUE

From the above code, we again proved that 5 is a numeric value, but it is not stored as an integer; it is stored as a double, and that’s why this function returns FALSE. The number 5 is mathematically an integer, but it is stored as a double.

Syntax

is.integer(obj)

Parameters

Name Value
obj It is an input object that will be checked for integer type.

Vector with integers

vec <- c(11L, 19L, 21L, 48L)

is.integer(vec) # Output: TRUE

You can see that all the elements in the vector are of type integer, and hence it returns TRUE.

Remember that it does not return a vector with the same length as an input vector; it just returns TRUE or FALSE based on a verification of the integer.

Vector with mixed types

Let’s create a vector of mixed types (integer and double).

vec <- c(11L, 19L, 21, 48L)

is.integer(vec) # Output: FALSE

In the above code, vec contains 21, which is of type double, and hence, the whole vector is double and not an integer.

That’s all!

Recent Posts

What is as.vector() Function in R

The as.vector() function converts an input R object into a vector. The object can be…

23 hours ago

is.na() Function: Checking for Missing Data in R

The is.na() function checks for missing values (NA) in the R object. It returns TRUE…

2 days ago

is.matrix() Function: Check If an Object is a Matrix in R

The is.matrix() is a built-in R function that checks whether an input object is a…

2 days ago

is.logical() Function: Check If a Value is Logical in R

R is.logical() function checks whether an input object's data type is logical. If the object…

3 days ago

is.list() Function: Check If an Object is List in R

The is.list() function in R checks if an input object is a list. It returns…

1 week ago

is.nan() Function: Handling NaN Values in R

The is.nan() is a built-in R function that checks if a vector or any valid…

1 week ago