Arrays in R are one of the data objects which store the data in multiple dimensions. Suppose if you want to create an array of dimensions (2,3,2), then it will return 2 arrays each having 2 rows and 3 columns. It’s important to know that arrays can store data of only one type. 

For all the array computations, we will make use of array() function along with the dimension (dim) parameter.

Syntax of Array function in R

Array: The arrays are the R data objects used to store data in different dimensions.

array(data,dim,dimnames)

Where,

Data = Input vector.

Dim = The dimensions of the array.

Dimnames = The names of the dimension (rows and column names)

A simple array in R programming

arrays in r

Let’s create arrays in R. We will be creating two vectors of different sizes and then pass those as input to the array function to create an array in R. Let’s roll!

#Creats vector with different size
v1 <- c(1,2,3)
v2 <- c(4,5,6,7,8)

#Creates an array of specified dimension
df<- array(c(v1,v2),dim(2,2,1))
, , 1

     [,1] [,2]
[1,]    1    3
[2,]    2    4

That’s great! Hold a while to appreciate yourself. You just created an array in R programming. You can see the dimension parameter reads as 2,2,1. This means 2 rows and 2 columns and the array count will be 1.

Adding dimension names to an arrays in R

In the above output, you can see that there are no row and column names present. They are indicated by numbers by default. But do you know that you can name them as you like?

Yes, R allows you to name the dimensions as per your likes. All you need to do is to specify the dimnames parameter with names.

#Creats vector with different size
v1 <- c(1,2,3)
v2 <- c(4,5,6,7,8)

#Creates an array of specified dimension
df<- array(c(v1,v2),dim(2,2,2))
, , 1

     [,1] [,2]
[1,]    1    3
[2,]    2    4

, , 2

     [,1] [,2]
[1,]    5    7
[2,]    6    8

We have created two arrays and let’s name the dimensions of those.

v1 <- c(1,2,3)
v2 <- c(4,5,6,7,8)
row.names<- c('row 1','row 2')
 column.names <- c('column 1','column 2')

df <- array(c(v1,v2),dim = c(2,2,2), dimnames = list(row.names,column.names))
df
, , 1

      column 1 column 2
row 1        1        3
row 2        2        4

, , 2

      column 1 column 2
row 1        5        7
row 2        6        8

It’s amazing. You have successfully named the dimensions of the arrays in R. It’s very easy to deal with arrays in R. Just play with parameters. They will do the magic for you.

Accessing elements in an Arrays in R

You have created an array. You have named the dimensions. Now let’s try to access the elements present in those arrays.

v1 <- c(1,2,3)
v2 <- c(4,5,6,7,8)
row.names<- c('row 1','row 2')
 column.names <- c('column 1','column 2')

df <- array(c(v1,v2),dim = c(2,2,2), dimnames = list(row.names,column.names))
df
, , 1

      column 1 column 2
row 1        1        3
row 2        2        4

, , 2

      column 1 column 2
row 1        5        7
row 2        6        8

We can use the same array that we named in the previous sections. Now we can try to extract the elements present in it.

#Returns 2nd row, 1st column of 2nd array
df[2,1,2]
#Second row, first column of both arrays
df[2,1,1&2]
#second row, second column of first array
df[2,2,1]
#First entire array
df[,,1]
#Second entire array
df[,,2]
6
2  6
1
 column 1 column 2
row 1        1        3
row 2        2        4
 column 1 column 2
row 1        5        7
row 2        6        8

That’s it. All you need to do is to specify the location of the elements and the function will return the values in just a second.

Array calculations in R programming

We need to use the apply() function to perform array computations. In this section let’s use apply() to find the sum of the first rows of both arrays in R.

, , 1

      column 1 column 2
row 1        1        3
row 2        2        4

, , 2

      column 1 column 2
row 1        5        7
row 2        6        8

These are the arrays that you have created so far. Let’s use them to make some arithmetic computations using apply in R.

#Adds row values of both arrays
array_computations <- apply(df,c(1),sum)

#Dispaly results 
array_computations
row 1    row 2 
   16          20 

The apply function will add all the elements in the first and second row to return the output. If you can see the syntax of the apply function, we will be giving data, margins, and functions to perform the calculations.

You can also multiply, divide, subtract the values suing apply function. It’s worth trying it.

Wrapping Up – Arrays in R programming

Arrays in R are the most useful data objects used to store data in multidimensions. You can create arrays, add the dimension names, access the elements from the arrays, and finally, you can do many mathematical computations on arrays using the apply() function in R.

That’s all for now. Happy R!!!

More read: R documentation

Categorized in: