R Basic

What is as.array() Function in R

If you are working with multidimensional data in R, you should explore Arrays. Arrays are designed to store data in more than two dimensions and represent complex data structures.

The as.array() function attempts to coerce the input object into an array structure. It handles various input types, including vectors, lists, data frames, etc.

Syntax

as.array(input_obj, ...)

Parameters

Name Value
input_obj It is an input object that will be converted into an array based on its class.
They are additional arguments depending on the class of input_obj.

Visual representation

Creating an array from a vector

If an input object is a vector for the as.array() function, it will return a one-dimensional array.

vec <- 1:10

arr <- as.array(vec)

print(arr)

print(class(arr))

Output

 [1] 1 2 3 4 5 6 7 8 9 10

 [1] "array"

You can see that we printed a one-dimensional array as well as its class, which is an “array”.

Creating an array from a matrix

If you pass a matrix as an argument to the as.array() function, it returns the matrix as an array with two dimensions.

mat <- matrix(1:9, nrow = 3, ncol = 3)

arr_frm_mat <- as.array(mat)

print(arr_frm_mat)

print(class(arr_frm_mat))

Output

You can see from the above figure that the matrix is now converted into a two-dimensional array. It is the same as the original input matrix.

Creating an array from a list

If you want to create an array from the list, you need to be careful considering the list’s structure. If list elements have the same length, it can be converted into an array.

main_list <- list(c(11, 21, 31), c(14, 15, 16), c(71, 81, 91))

arr_frm_list <- as.array(unlist(main_list))

dim(arr_frm_list) <- c(3, 3)

print(arr_frm_list)

print(class(arr_frm_list))

Output

In this code, we first unlist to create a vector from a list and then set the dimensions using dim() to reshape it into a 3×3 array.

Conclusion

If your operation requires an array and you currently have either vectors, matrices, or lists, you can create an array out of it using the as.array() function. It provides more control over dimensions and reshapes the data.

If you want to check if an input object is an array, use the is.array() function.

Recent Posts

How to Create an Empty Vector and Append Values in R

R vectors are atomic, which means they have homogeneous data types. They are contiguous in…

7 hours ago

How to Remove Single and Multiple Columns from Data Frame in R

DataFrames are like tables that contain rows and columns. Each column can have a different…

1 day ago

How to Convert Date to Numeric in R

Dates in R are stored as the number of days since 1970-01-01, so converting a…

2 days ago

How to Create a Data Frame from Vectors in R

In R, you can think of a vector as a series of values in a…

2 weeks ago

R dplyr::filter() Function: Complete Guide

The dplyr filter() function in R subsets a data frame and retains all rows that…

2 weeks ago

R distinct() Function from dplyr

The dplyr::distinct() function in R removes duplicate rows from a data frame or tibble and keeps…

2 weeks ago