R Arrays – A Comprehensive Guide to Array with Examples
This TechVidvan tutorial is designed to help R programming beginner to get a clear understanding of how to easily work with R arrays.
Arrays are multi-dimensional data structures in R. This means that they can have any number of dimensions. They are vectors with two or more dimensions to them. Matrices are a special case of 2-D arrays.
In this tutorial, we will explore the concept of arrays in R programming. We will learn to create arrays, name rows, columns, and other dimensions. We will then look at accessing their elements and modifying them.
Finally, we will look at how different operations on arrays work in the R programming language.
So, are you excited?
What are arrays in R?
R arrays are data objects that store data in more than two dimensions. Arrays are homogeneous in nature. This means that they can store values of a single basic data type only. They store the data in the form of layered matrices. Let us take a look at different ways of creating an array.
But before learning how to create an array in R, first look at the syntax of the array function.
Array function syntax
The syntax of the array()
function is as follows:
Array_name = array(data,dim = c(row_size,column_size,matrices), dimnames = list(row_names,column_names,matrices_names))
Where data
is a vector that provides the values to fill the array,
dim
is a vector that tells the dimensions of the array,
row_size
is the number of rows in the array,
column_size
is the number of columns in the array,
matrices
denotes the number of matrices in the array,
dimnames
is a list of names for the dimensions of the array,
row_names
is a vector with the names for all the rows,
column_names
is a vector with the names for all the columns,
matrices_names
is a vector with the names for all the matrices in the array.
Creating R arrays
There are many ways to create R arrays. We can use the array()
function to create an array.
For example:
Code:
> arr1 <- array(c(1:18),dim=c(2,3,3)) > arr1
Output:
We can also create an array by changing the dimensions of a vector. For example:
Code:
> vec_to_arr <- c(1,2,3,4,5,6,7,8) > dim(vec_to_arr) <- c(2,2,2) > vec_to_arr
Output:
Naming the dimensions of R arrays
We can name the rows, columns, and matrices of an array during its creation by using the dimnames
attribute of the array()
function. For example:
Code:
> rnames <- c("r1","r2","r3") > cnames <- c("c1","c2","c3") > mnames <- c("m1","m2","m3") > named_array <- array(c(1:27),dim=c(3,3,3),dimnames= list(rnames,cnames,mnames)) > named_array
Output:
We can use the dimnames()
function to name the dimensions and to change their names after an array’s creation. For example:
Code:
> named_array2 <- array(c(1:18),c(3,3,2)) > mnames2 <- c("m1","m2") > dimnames(named_array2) <- list(rnames,cnames,mnames2) > named_array2
Output:
Indexing R array
We can access the elements of an array by using the square brackets to denote an index. We can use four types of indices for this, which are positive integers, negative integers, logical values or characters.
Using positive integers
Code:
> named_array[2,1,3] #second row, first column and third matrix of named_array
Code:
> named_array[c(1,2),c(2,3),c(1,2)] #first and second row, second and third column of first and second matrices
Output:
Using negative integers
Code:
> named_array[-1,-3,-2] #remove first row, third column and second matrix of named_array
Output:
Using logical vectors
Code:
> named_array[c(T,F,T),c(F,T,T),c(T,T,F)] #first and third row, second and third column of first and second matrix of named_array
Output:
Logical index vectors recycle, in case they are shorter than the array dimensions. For example:
Code:
>named_array[c(T,F),c(F,T),c(T,T,F),drop=FALSE]
Output:
Note: If we select a single entry of any dimension of an array, the result will be a matrix. To avoid that, we use drop=FALSE
argument.
Using character vectors
Code:
> named_array[c("r1","r3"),c("c2","c3"),c("m1","m2")]
Output:
Modifying R array
We can use the indexing techniques to access elements or parts of an array. Then we can use re-assignment to change their values. For example:
Code:
> named_array[1,,1] <- c(4,4,4) > named_array
Output:
We can also use the dim()
function to change the dimensions of an array. For example:
Code:
> dim(arr1) <- c(3,3,2) > arr1
Output:
R Array arithmetic
In R, the basic operations of addition, subtraction, multiplication, and division work element-wise. We need to ensure that the arrays are of the proper size and valid according to matrix arithmetic.
For example, the number of rows of the first matrix and the number of columns of the second matrix should be the same for multiplication.
1. Addition
Code:
> test_arr1 <- array(c(1:8),c(2,2,2)) > test_arr2 <- array(c(9:16),c(2,2,2)) > arr_add <- test_arr1+test_arr2 > arr_add
Output:
2. Subtraction
Code:
> arr_sub <- test_arr2-test_arr1 > arr_sub
Output:
3. Multiplication
Code:
> arr_mul <- test_arr1*test_arr2 > arr_mul
Output:
4. Division
Code:
> arr_div <- test_arr2/test_arr1 > arr_div
Output:
5. Power operator
The power operator^
works on R arrays in the same fashion that is element-wise.
Code:
> arr_pow <- test_arr1^2 > arr_pow
Output:
6. The apply() Function
We can also perform operations on array elements with the apply()
function. The apply function performs a given function on all columns or all rows of an array or matrix.
For example, the following code gives the sums of all the columns of test_arr1
.
Code:
> test_arr1
Code:
> col_sum <- apply(test_arr1,2,sum) > col_sum
Output:
Summary
In this tutorial, we learned about the arrays in R programming. We learned about their creation, modification, and manipulation. We saw how to access their elements. Finally, we looked at a few basic mathematical operations on arrays.
R Arrays are special vectors with multiple dimensions to them. They store multidimensional rectangular data.
Now you must be realizing how important R vector is, take a tour to R vectors and master the concept.
The above article should give you a clear understanding of arrays in the R programming language.
Confusion in R array examples? Ask TechVidvan in the comment section.
Keep learning!!