What is the use of arrays in r?

Asked

Viewed 423 times

5

vector, matrix, data.frame and list are widely used in data analysis and tag peguntas r here in the website. In particular, I see no applicability of array in r. For example:

vec_1 <- c(2:4)
vec_2 <- c(15:20)

result <- array(c(vec_1, vec_2), dim = c(3, 3, 2))

result

, , 1

     [,1] [,2] [,3]
[1,]    2   15   18
[2,]    3   16   19
[3,]    4   17   20

, , 2

     [,1] [,2] [,3]
[1,]    2   15   18
[2,]    3   16   19
[3,]    4   17   20

class(result)
[1] "array"
  • what is the logic behind the creation of a array (as I did in the example above)?

  • in which situations the use of array is useful in r?

  • how to extract the matrices present in array?

1 answer

4


An array, in the R, is a generalized form of the matrix.

An array has only one dimension: size. The matrix has only two dimensions: rows and columns. An array can be n dimensions.

Vector case

library(magrittr)
array(1:12) %>% 
  print() %>% 
  dim()

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

Matrix case

array(1:12, c(3, 4)) %>% 
  print() %>% 
  dim()

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

General case

array(1:12, c(2, 3, 2)) %>% 
  print() %>% 
  dim()

, , 1

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

, , 2

     [,1] [,2] [,3]
[1,]    7    9   11
[2,]    8   10   12

[1] 2 3 2

General case with 4 dimensions

array(1:16, c(2, 2, 2, 2)) %>% 
  print() %>% 
  dim()

, , 1, 1

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

, , 2, 1

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

, , 1, 2

     [,1] [,2]
[1,]    9   11
[2,]   10   12

, , 2, 2

     [,1] [,2]
[1,]   13   15
[2,]   14   16

[1] 2 2 2 2

So yes, it’s useless in most cases because most cases we want to deal with these specific cases of arrays which are the vectors and matrices.

what is the logic behind the creation of an array (as I did in the above example)?

The logic is the same as the matrix. In the case given above the matrix was duplicated because it was recycled, you asked to create an array with 18 elements (3 * 3 * 2), but provided only 9, so they were recycled.

in which situations the use of the array is useful in r?

An array can be useful when you need more than 2 dimensions. Imagine that you want to create a shape to view and solve Magic Cubes (Rubik’s Cube). In this case an array or an array would not be enough and we would end up using an array of dimensions c(3, 3, 6). Another possible application is to read color images that are composed of 3 channels, each with an array of color intensity. In this case an image with 800x600 pixel would have the dimensions c(800, 600, 3).

img <- jpeg::readJPEG(system.file("img", "Rlogo.jpg", package="jpeg"))
str(img)
# num [1:76, 1:100, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
is.array(img)
# [1] TRUE

how to extract the arrays present in the array?

Taking the last array as an example, it is possible to make a subset as we do with matrices, but respecting the number of dimensions of the array.

meu_array <- array(1:16, c(2, 2, 2, 2))
meu_array[2, 2, , ]
     [,1] [,2]
[1,]    4   12
[2,]    8   16

In the example above we select the second row of the second column of each of the third and fourth dimensions.

If the number of dimensions is not respected, we will have an error.

meu_array[2, 2]
Error in meu_array[2, 2] : incorrect number of dimensions
  • 3

    Another example: the first time I had to use arrays was in a project involving simulations. It had several estimators for a certain parameter, let’s say E estimators and their variances. This gives a matrix 2*E. Now run R times and save the results in an array 2*E*R.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.