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

R dplyr::slice() Function

The dplyr::slice() function subsets rows by their position or index within a data frame. If…

19 hours ago

How to Create an Empty Vector and Append Values in R

R vectors are atomic, which means they have homogeneous data types. They are contiguous in…

2 days ago

How to Remove Single and Multiple Columns from Data Frame in R

DataFrames are like tables that contain rows and columns. Each column can have a different…

2 days ago

How to Convert Date to Numeric in R

Dates in R are stored as the number of days since 1970-01-01, so converting a…

4 days ago

How to Create a Data Frame from Vectors in R

In R, you can think of a vector as a series of values in a…

2 weeks ago

R dplyr::filter() Function: Complete Guide

The dplyr filter() function in R subsets a data frame and retains all rows that…

2 weeks ago