R – Check if Type of Vector is Integer

To check if type of given vector is integer in R, call is.integer() function and pass the vector as argument to this function. If the given vector is of type integer, then is.integer() returns TRUE, or else, it returns FALSE.

The syntax to call is.integer() to check if type of vector x is integer is

</>
Copy
is.integer(x)

Return Value

The function returns a logical value.

Examples

In the following program, we take a vector in x, and check if this vector is a integer vector using is.integer() function. We print the value returned by is.integer().

example.R

</>
Copy
x <- c(25L, 84L, 10L)
result <- is.integer(x)
print(result)

Output

[1] TRUE

Now, let us take a vector x of some type other than integer, and check the value returned by is.integer(x). Since x is not of type integer, is.integer(x) must return FALSE.

example.R

</>
Copy
x <- c("2", "a", "mno")
result <- is.integer(x)
print(result)

Output

[1] FALSE

Conclusion

In this R Tutorial, we learned how to check if given vector is of type integer, using is.integer() function, with the help of examples.