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

How to Check If File and Folder Already Exists in R

Whether you are reading or writing files via programs in the file system, it is…

1 day ago

How to Check Data type of a Variable in R

When it comes to checking the data type of a variable, it depends on what…

2 days ago

Mastering grepl() Function in R

The grepl() function (stands for "grep logical") in R searches for patterns within each element…

3 days ago

zip(), unzip() and tar(), untar() Functions in R

The zip() function creates a new zip archive file. You must ensure that the zip tool…

4 days ago

How to Create Directory and File If It doesn’t Exist in R

When working with file systems, checking the directory or file existence is always better before…

5 days ago

How to Create a Grouped Boxplot in R

To create a grouped boxplot in R, we can use the ggplot2 library's aes() and…

7 days ago