Check if the Object is a Matrix in R Programming – is.matrix() Function

is.matrix() Function in detail

The is.matrix() function in R is used to determine whether a given object is a matrix. It returns TRUE if the object is a matrix and FALSE otherwise.

Syntax:

is.matrix(x)

Parameters:

  • x: The object to be checked.

Example 1: Checking Different Matrices

# R program to demonstrate is.matrix() function

# Creating matrices
mat1 <- matrix(1:6, nrow = 2)
mat2 <- matrix(1:9, nrow = 3, byrow = TRUE)
mat3 <- matrix(seq(1, 16, by = 2), nrow = 4)

# Applying is.matrix() function
is.matrix(mat1)
is.matrix(mat2)
is.matrix(mat3)

Output:

[1] TRUE
[1] TRUE
[1] TRUE

Example 2: Checking Different Data Types

# R program to check different data types

# Creating a dataset
data_obj <- mtcars

# Applying is.matrix() function
is.matrix(data_obj)

# Checking non-matrix elements
is.matrix(10)
is.matrix(TRUE)
is.matrix("Hello")

Output:

[1] FALSE
[1] FALSE
[1] FALSE
[1] FALSE

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *