R – Type of Vector

To programmatically get type of a vector in R, call typeof() function and pass the vector to it as argument.

In this tutorial, we will learn how to find the type of a given vector using typeof() function, with examples.

Syntax

The syntax to use typeof() function to get the data type of vector x is

typeof(x)

The function returns a character string representing the type of the given vector.

ADVERTISEMENT

Examples

In the following program, we create a vector of type integer, and find the type using typeof() function.

example.R

x <- c(1L, 2L, 4L, 8L)
print(typeof(x))

Output

[1] "integer"

Now, let us create a vector with logical values, and find this vector’s type using typeof().

example.R

x <- c(TRUE, TRUE, FALSE)
print(typeof(x))

Output

[1] "logical"

Conclusion

In this R Tutorial, we learned how to find the type of a given vector, using typeof() function, with the help of examples.