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.
is.integer(obj)
Name | Value |
obj | It is an input object that will be checked for integer type. |
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.
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!
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
The as.vector() function converts an input R object into a vector. The object can be…
The is.na() function checks for missing values (NA) in the R object. It returns TRUE…
The is.matrix() is a built-in R function that checks whether an input object is a…
R is.logical() function checks whether an input object's data type is logical. If the object…
The is.list() function in R checks if an input object is a list. It returns…
The is.nan() is a built-in R function that checks if a vector or any valid…