How To Get Type of Vector in R?
We use typeof() function to programmatically get type of a vector in R by passing vector as the argument. The function returns a character string as the type of the vector.
The Syntax:
typeof(x) |
Example:
#vector of integer values
x <- c(1L, 5L, 3L, 7L)
print(typeof(x))
#vector of double values
x <- c(10, 50, 30, 80)
print(typeof(x))
#vector of character values
x <- c("p", "q", "r", "s")
print(typeof(x))
#vector of logical values
x <- c(TRUE, TRUE, FALSE)
print(typeof(x))
#output:
[1] "integer"
[1] "double"
[1] "character"
[1] "logical"
Conclusion:
To get type of vector is done by calling typeof() function and passing the vector in to it.
