R – Check if Type of Vector is Numeric

To check if type of given vector is numeric in R, that is either integer or double, call is.numeric() function and pass the vector as argument to this function.

If the given vector is of type integer or double, then is.numeric() returns TRUE, or else, it returns FALSE.

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

</>
Copy
is.numeric(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 numeric vector using is.numeric() function. We print the value returned by is.numeric().

example.R

</>
Copy
#integer vector
x <- c(2L, 4L, 6L, 8L)
result <- is.numeric(x)
print(result)

#double vector
x <- c(1, 2, 4, 8)
result <- is.numeric(x)
print(result)

Output

[1] TRUE
[1] TRUE

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

example.R

</>
Copy
x <- c("Hello", "World")
result <- is.numeric(x)
print(result)

Output

[1] FALSE

Conclusion

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