R Basic

What is is.array() Function in R

The is.array() is a built-in R function that checks whether an input object is an array. It returns TRUE if it is an array and FALSE otherwise.

Syntax

is.array(obj)

Parameters

Name Value
obj It is an input object that will be checked for an array object.

Visual representation

Checking for an array object

# Creating an array
arr <- array(1:9, dim = c(3, 3, 3))

# Checking if it is an array
is.array(arr) # Output: TRUE

Since an “arr” is an array, it returns TRUE.

Let’s check for a vector.

 

 

# Creating a vector
vec <- c(1, 2, 3, 4, 5)

# Checking if "vec" is an array
is.array(vec) # Output: FALSE

And as expected, we got the FALSE output.

Checking matrix object

Matrix is a special case in R because it is an array with two dimensions. Therefore, is.array() will return TRUE for matrices as well.

# Create a matrix
mtrx <- matrix(1:9, nrow = 3)

# Checking if 'mtrx' is an array
is.array(mtrx) # Output: TRUE

# Checking the class of 'mtrx'
class(mtrx) # Output: "matrix" "array"

You can say that all matrices are arrays, but not all arrays are matrices in R.

Checking with NA and NaN

If you create an array containing both NA and NaN values, the is.array() function correctly returns TRUE.

# Create an array with NA and NaN
arr_na_nan <- array(c(1, NA, 3, NaN, 5, 6), dim = c(2, 3))

# Check if it's an array
is.array(arr_na_nan) # Output: TRUE

That’s all!

Recent Posts

Splitting Strings: A Beginner’s Guide to strsplit() in R

The strsplit() function in R splits elements of a character vector into a list of…

13 hours ago

Understanding of rnorm() Function in R

The rnorm() method in R generates random numbers from a normal (Gaussian) distribution, which is…

6 days ago

as.factor() in R: Converting a Vector to Categorical Data

The as.factor() function in R converts a vector object into a factor. Factors store unique…

6 days ago

cbind() Function: Binding R Objects by Columns

R cbind (column bind) is a function that combines specified vectors, matrices, or data frames…

3 weeks ago

rbind() Function: Binding Rows in R

The rbind() function combines R objects, such as vectors, matrices, or data frames, by rows.…

3 weeks ago

as.numeric(): Converting to Numeric Values in R

The as.numeric() function in R converts valid non-numeric data into numeric data. What do I…

4 weeks ago