R Array

[This article was first published on R feed, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

An Array is a data structure which can store data of the same type in more than two dimensions.

The only difference between vectors, matrices, and arrays are

  • Vectors are uni-dimensional arrays
  • Matrices are two-dimensional arrays
  • Arrays can have more than two dimensions

Before we learn about arrays, make sure you know about R matrix and R vector.


Create an Array in R

In R, we use the array() function to create an array.

The syntax of the array() function is

array(vector, dim = c(nrow, ncol, nmat))

Here,

  • vector – the data items of same type
  • nrow – number of rows
  • ncol – number of columns
  • nmat – the number of matrices of nrow * ncol dimension

Let's see an example,

# create two 2 by 3 matrix
array1 <- array(c(1:12), dim = c(2,3,2))

print(array1)

Output

, , 1

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

, , 2

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

In the above example, we have used the array() function to create an array named array1. Notice the arguments passed inside array(),

array(c(1:15), dim = c(2,3,2))

Here,

  • c(1:12) - a vector with values from 1 to 12
  • dim = c(2,3,2) - create two matrices of 2 by 3 dimension

Finally, the numbers from 1 to 12 that are arranged in two 2 by 3 matrices are printed.


Access Array Elements

We use the vector index operator [ ] to access specific elements of an array in R.

The syntax to access an array element is

array[n1, n2, mat_level]

Here,

  • n1 - specifies the row position
  • n2 - specifies the column position
  • mat_level - specifies the matrix level

Let's see an example,

# create two 2 by 3 matrix
array1 <- array(c(1:12), dim = c(2,3,2))

print(array1)

# access element at 1st row, 3rd column of 2nd matrix
cat("\nDesired Element:", array1[1, 3, 2])

Output

, , 1

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

, , 2

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


Desired Element: 11

In the above example, we have created an array named array1 with two 2 by 3 matrices. Notice the use of index operator [],

array1[1, 3, 2]

Here, [1, 3, 2] specifies we are trying to access element present at the 1st row, 3rd column of the 2nd matrix i.e. 11.

Access Entire Row or Column

In R, we can also access the entire row or column based on the value passed inside [].

  • [c(n), ,mat_level] - returns the entire element of the nth row.
  • [ ,c(n), mat_level] - returns the entire element of the nth column.

For example,

# create a two 2 by 3 matrix
array1 <- array(c(1:12), dim = c(2,3,2))

print(array1)


# access entire elements at 2nd column of 1st matrix
cat("\n2nd Column Elements of 1st matrix:", array1[,c(2),1])

# access entire elements at 1st row of 2nd matrix
cat("\n1st Row Elements of 2nd Matrix:", array1[c(1), ,2])

Output

, , 1

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

, , 2

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


2nd Column Elements of 1st matrix: 3 4
1st Row Elements of 2nd Matrix: 7 9 11

Here,

  • array1[,c(2),1] - access 2nd column elements of 1st matrix
  • array1[c(1), ,2] - access 1st row of 2nd matrix

Check if Element Exists

In R, we use the %in% operator to check if the specified element is present in the matrix or not and returns a boolean value.

  • TRUE - if specified element is present in the matrix
  • FALSE - if specified element is not present in the matrix

For example,

# create a two 2 by 3 matrix
array1 <- array(c(1:12), dim = c(2,3,2))

11 %in% array1 # TRUE

13 %in% array1 # FALSE

Output

[1] TRUE
[2] FALSE

Here,

  • 11 is present in array1, so the method returns TRUE
  • 13 is not present in array1, so the method returns FALSE

#length Length of Array in R

In R, we can use the length() function to find the number of elements present inside the array. For example,

# create a two 2 by 3 matrix
array1 <- array(c(1:12), dim = c(2,3,2))

# find total elements in array1 using length()
cat("Total Elements:", length(array1))

Output

Total Elements: 12

Here, we have used length() to find the length of array1. Since there are two 2 by 3 matrices the length() function returns 12.

To leave a comment for the author, please follow the link and comment on their blog: R feed.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)