R – Check if given Object is a Vector
In this tutorial, we will learn how to check if given object is a vector in R programming Language.
To check if given object is a vector in R, call is.vector()
function and pass the given object as argument to it. If the given object x
is of type vector, then is.vector(x)
returns TRUE
, else it returns FALSE
.
The syntax to check if object x
is a vector is
is.vector(x)
Example
Positive Scenario – Given object is a Vector
In this example, we take a vector in x
, and programmatically check if x
is vector or not using is.vector()
function.
Example.R
x = c("A", "B", "C")
output = is.vector(x)
if (output) {
print("x is a vector.")
} else {
print("x is not a vector.")
}
Output
[1] "x is a vector."
Positive Scenario – Given object is not a Vector
In this example, we take a data frame in x
, and programmatically check if x
is vector or not using is.vector()
function.
Example.R
x = data.frame(x1 = c("A", "B", "C"))
output = is.vector(x)
if (output) {
print("x is a vector.")
} else {
print("x is not a vector.")
}
Output
[1] "x is not a vector."
Conclusion
In this R Tutorial, we learned how to check if given object is a vector or not using is.vector()
, with the help of example programs.