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.
is.array(obj)
Name | Value |
obj | It is an input object that will be checked 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.
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.
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!
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
The paste() function in R concatenates vectors after converting them to character. paste("Hello", 19, 21,…
R paste0() function concatenates strings without any separator between them. It is a shorthand version…
Standard Error (SE) measures the variability or dispersion of the sample mean estimate of a…
max() The max() function in R finds the maximum value of a vector or data…
The as.Date() function in R converts various types of date and time objects or character…
The pnorm() function in R calculates the cumulative density function (cdf) value of the normal…