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 paste() Function

The paste() function in R concatenates vectors after converting them to character. paste("Hello", 19, 21,…

7 days ago

paste0() Function in R

R paste0() function concatenates strings without any separator between them. It is a shorthand version…

1 week ago

How to Calculate Standard Error in R

Standard Error (SE) measures the variability or dispersion of the sample mean estimate of a…

2 weeks ago

R max() and min() Functions

max() The max() function in R finds the maximum value of a vector or data…

2 weeks ago

R as.Date() Function: Working with Dates

The as.Date() function in R converts various types of date and time objects or character…

2 weeks ago

R pnorm() Function [With Graphical Representation]

The pnorm() function in R calculates the cumulative density function (cdf) value of the normal…

3 weeks ago