# Operations on Arrays in R — All you need to know!!

Filed Under: R Programming Hello, readers! In this article, we will be focusing on an important data structure in R – Operations on Arrays in R, in detail.

This post would guide you through the basics of Arrays as well as the operations that can be performed on arrays in R programming.

So, let us begin!! 🙂

## What are Arrays in R?

Be it any programming language, data structures play an important role in analyzing the problems and driving a way towards solution. Array is one such data structure.

In R programming, Arrays are objects that are used for the storage of data. They store the data in more than two dimensions. Being homogeneous in nature, arrays store only similar types of elements at once.

Now, let us have a look at the creation of array in R programming.

### Creation of an Array in R

In order to create an array in R, we need to make use of `array()` function.

Let us have a look at the below syntax!

```array_name<- array(data, dim = (row_Size, column_Size, matrices, dimnames)
```
• data: It contains the data in terms of rows and column values.
• row_size: The number of row elements that can be stored by an array.
• column_size: The number of column elements that can be stored by an array.
• dimnames: Customizable way to change the headers/names of the rows and columns.
• matrices: Let us have multi dimensional matrices.

Example:

```x <- c(1, 2, 3,4,5,6,7,8)
y <- c(10, 20, 30, 40, 50, 60)

arr <- array(c(x, y), dim = c(4,4,2))
print(arr)

```

Here, we have created an array of the list x and y, with 4 elements per row and column along with 2 versions of matrices.

Output:

```, , 1

[,1] [,2] [,3] [,4]
[1,]    1    5   10   50
[2,]    2    6   20   60
[3,]    3    7   30    1
[4,]    4    8   40    2

, , 2

[,1] [,2] [,3] [,4]
[1,]    3    7   30    1
[2,]    4    8   40    2
[3,]    5   10   50    3
[4,]    6   20   60    4

```

### 1. Naming rows and columns of an Array in R

At first, we can manipulate the names or headers of the rows and columns of an array in a user-defined manner. We just need to add the names into the `dimnames` parameter of the function.

Example:

In this example, we have created a list of the names for rows, columns, and matrix and then have passed it as parameter values to the function.

```x <- c(5,6,7,8)
y <- c(10, 20, 30, 40, 50, 60)

column.names <- c("A", "B", "C")
row.names <- c("R1", "R2", "R3")
matrix.names <- c("NO1", "NO2")

arr <- array(c(x, y), dim = c(3,3,2),dimnames = list(row.names, column.names,matrix.names))
print(arr)
```

Output:

```, , NO1

A  B  C
R1 5  8 30
R2 6 10 40
R3 7 20 50

, , NO2

A  B  C
R1 60  7 20
R2  5  8 30
R3  6 10 40
```

### 2. Manipulation of data values of an Array

We can even perform arithmetic operations on the arrays in R programming. For the same, we extract the created arrays as matrices and then perform manipulations on the same, as shown below!

Example:

```x <- c(5,6,7,8)
y <- c(10, 20, 30, 40, 50, 60)

arrr <- array(c(x, y), dim = c(3,3,2))
print(arrr)

a <- c(1,2,3,4)
b <- c(50,60,70,80,90,100)

arr1 <- array(c(a, b), dim = c(3,3,2))
print(arr1)

val1 <- arrr[,,2]
val2 <- arr1[,,2]
res <- val1 * val2
print(res)
```

Here, we have performed multiplication operation on the arrays and have printed the resultant array.

Output:

```, , 1

[,1] [,2] [,3]
[1,]    1    4   70
[2,]    2   50   80
[3,]    3   60   90

, , 2

[,1] [,2] [,3]
[1,]  100    3   60
[2,]    1    4   70
[3,]    2   50   80

> print(res)
[,1] [,2] [,3]
[1,] 6000   21 1200
[2,]    5   32 2100
[3,]   12  500 3200
```

### 3. Accessing elements of an R Array

At times, while working on some data, we come across situations wherein we need only some portion of data in return from the data structure. Well, this can be achieved within arrays.

Syntax:

```array[row_num,column_num,matrix_level]
```

Using the above syntax, we can extract the specific row or column values of a mentioned matrix level.

In the below example, we have extracted the below portions from the array:

• The second row of the second matrix level from the array.
• The second column of the first matrix level from the array.

Example:

```x <- c(5,6,7,8)
y <- c(10, 20, 30, 40, 50, 60)

arrr <- array(c(x, y), dim = c(3,3,2))
print(arrr)

print(arrr[2,,2]) # prints 2nd row of 2nd matrix
print(arrr[,2,1]) # prints 2nd column of 1st matrix

```

Output:

```> print(arrr)
, , 1

[,1] [,2] [,3]
[1,]    5    8   30
[2,]    6   10   40
[3,]    7   20   50

, , 2

[,1] [,2] [,3]
[1,]   60    7   20
[2,]    5    8   30
[3,]    6   10   40

> print(arrr[2,,2])
  5  8 30
> print(arrr[,2,1])
  8 10 20
```

## Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to R programming, stay tuned.

Till then, Happy Learning!! 🙂

close
Generic selectors
Exact matches only
Search in title
Search in content