What is the is.vector() function in R?
Overview
The is.vector() function in R is used to test if a given R object is a vector or not.
Syntax
The syntax for the is.vector() method:
is.vector(x, mode = "any")
Syntax for the is.vector() function
Parameter value
The is.vector() function takes the following parameter values:
x: This is an R object.mode: This is a character string naming an atomic mode. This is an optional parameter.
Return value
The is.vector() function returns a logical value, (TRUE or FALSE), indicating whether the R object is a vector or not.
Code example
The code below is an example of the is.vector() method:
# creating different R objectsmy_vector1 <- c('one', 'two', 'three')my_vector2 <- c('A', 'B', 'C')my_vector3 <- matrix(c("orange", "mango", "pineapple", "watermelon"), nrow = 2, ncol = 2)# testing if they are vectors or notmy_vector1is.vector(my_vector1)my_vector2is.vector(my_vector2)my_vector3is.vector(my_vector3)
Explanation
In the code above, we see the following:
- Lines 2–4: We create different R objects,
my_vector1,my_vector2, andmy_vector3. - Line 7: We print the value of the object,
my_vector1. - Line 8: We test if the object,
my_vector1object, is a vector or not by using theis.vector()function. - Line 10: We print the value of the object,
my_vector2. - Line 11: We test if the object,
my_vector2object, is a vector or not by using theis.vector()function. - Line 12: We print the value of the object,
my_vector3. - Line 13: We test if the object,
my_vector3, object is a vector or not by using theis.vector()function.