The is.matrix()
function in R will return a logical value (TRUE
or FALSE
), indicating whether or not an argument passed to it is a vector with a dim attribute of length 2
.
is.matrix(x)
The is.matrix()
function takes a single parameter value, x
, representing any R object.
The is.matrix()
function returns TRUE
if the argument is a vector
and has an attribute dim
of length 2
. Otherwise, it returns FALSE
.
# creating R objectsmymatrix <- matrix(c("apple", "banana", "cherry", "grape", "melon", "pineapple"), nrow = 3, ncol = 3)myvector <- c("apple", "banana", "cherry", "grape", "melon", "pineapple")# checking if the objects is/are a matrixis.matrix(mymatrix)is.matrix(myvector)# obtaining their dim attributedim(mymatrix)dim(myvector)
mymatrix
, using the matrix()
function.myvector
.is.matrix()
function on the object, mymatrix
to see if it's a matrix or not.is.matrix()
function on the object, myvector
to see if it's a matrix or not.dim
attribute of both objects.Note: The attributedim
for the object,myvector
, is not of length2
, unlike the other matrix object with a length of2
. To makemyvector
into a matrix object, we only need to pass thedim
attribute.
# creating R objectsmymatrix <- matrix(c("apple", "banana", "cherry", "grape", "melon", "pineapple"), nrow = 3, ncol = 3)myvector <- c("apple", "banana", "cherry", "grape", "melon", "pineapple")# passing an attribute to the vector objectdim(myvector) <- c(2,3)# Now check to see if it is a matrix object or notis.matrix(myvector)
mymatrix
, using the matrix()
function.myvector
.dim
attribute, we passed a dimension of 2
by 3
to the object to make it a matrix with dim
attribute of length 2
.is.matrix()
function, we check to see if the object, myvector
, is a matrix object or not.