Hello Ninjas, we have been learning about arrays from the very start of our DSA journey, and it plays an important role where data can be stored in the form of rows and columns; our data can be accessed using row index and column index.

Array in R Programming Language are the data objects which can store the data in more than two dimensions; let's say we create an array of dimensions (3, 3, 2), then it creates 2 rectangular matrices with 3 rows and 3 columns. Arrays can store the values having only a similar kind of data type.

An array is created using the array() function, which will take vectors as input and use the values in the dim parameter.

Furthermore, we will go through its implementation, properties, and operations to understand this concept in a better way.

As we know, Arrays in R programming are created using the array() function. So before going through the implementation part, let's see its syntax and the arguments it can take.

Syntax:

Array_NAME <- array(data, dim = (row_size, col_size, matrices, dimnames)

data – It is an input vector that is given to the array.

row_size – It describes the number of array elements an array can store.

col_size – It describes the number of column elements stored in an array.

matrices – Array in R programming consists of multi-dimensional matrices.

dimnames – It is used to change the default names given to rows and columns according to the user's choice.

Arguments in Array

The array() in R programming can be written as:

array(data = A, dim = length(data), dimname = NULL)

data- It is an input vector.

dim- It is an attribute that provides maximum indices in each dimension

dimname- It can be either NULL or have a name for the array.

Example:

In the example below, we will create an array of three 2x2 matrices having 2 rows and 2 columns.

# Two vectors of different lengths.
vec1 <- c(4, 5, 6, 7)
vec2 <- c(8, 9, 10, 11, 12)
# Take these vectors as mentioned above as input to the array.
res <- array(c(vec1,vec2),dim = c(2,2,3))
print(res)

Output:

Explanation:

In the above-mentioned example, we have created two vectors of different lengths, and then three arrays are created of dimensions 2x2.

Get the tech career you deserve, faster!

Connect with our expert counsellors to understand how to hack your way to success

User rating 4.7/5

1:1 doubt support

95% placement record

Akash Pal

Senior Software Engineer

326% Hike After Job Bootcamp

Himanshu Gusain

Programmer Analyst

32 LPA After Job Bootcamp

After Job Bootcamp

Operations on Array in R Programming

In this section, we will discuss different operations that can be performed on an array in R programming and their implementation in detail

Naming Columns and Rows

We can give names to the rows and columns using dimnames.

# Creating two Vectors of different lengths
vec1 <- c(4, 5, 6, 7)
vec2 <- c(8, 9, 10, 11, 12)
# Giving names to rows and columns
col.names <- c("C1", "C2", "C3")
row.names <- c("R1", "R2", "R3")
matrix.names <- c("Matrix1", "Matrix2")
res <- array(c(vec1, vec2), dim = c(3, 3, 2), dimnames = list(row.names, col.names, matrix.names))
print(res)

Output:

Explanation:

In the above code, we have given names to the rows and columns of our two matrices, each of dimension 3x3.

Accessing Array elements

We can access the array elements after giving vectors as input to the array.

# Creating two Vectors
vec1 <- c(4, 5, 6, 7)
vec2 <- c(8, 9, 10, 11, 12)
# Giving names to rows and columns
col.names <- c("C1", "C2", "C3")
row.names <- c("R1", "R2", "R3")
matrix.names <- c("Matrix1", "Matrix2")
res <- array(c(vec1,vec2),dim = c(3,3,2),dimnames = list(row.names,
col.names, matrix.names))
# Print the first matrix
print(res[,,1])
# Print the element in the 2nd row and 3rd column of the 2nd matrix.
print(res[2,3,2])

Output:

Explanation:

We have created two vectors of different lengths, and after creating two vectors, we will take these vectors as input to the array and print the first matrix and the element in the 2nd row and 3rd column of the second matrix.

Manipulating Array Elements

As we know array in R programming is made up of matrices in multiple dimensions so that the operations on elements have been carried out by accessing the elements.

# Creating two Vectors
vec1 <- c(4, 5, 6, 7)
vec2 <- c(8, 9, 10, 11, 12)
# Giving names to rows and columns
col.names <- c("C1", "C2", "C3")
row.names <- c("R1", "R2", "R3")
matrix.names <- c("Matrix1", "Matrix2")
arr1 <- array(c(vec1,vec2),dim = c(3,3,2))
# Create another two vectors
vec3 <- c(1, 2, 3)
vec4 <- c(7, 8, 3, 9, 5, 0)
arr2 <- array(c(vec3,vec4),dim = c(3,3,2))
# Create matrices from arr1 and arr2.
matrix1 <- arr1[,,2]
matrix2 <- arr2[,,2]
# Add and print the result
res <- matrix1+matrix2
print(res)

Output:

Explanation:

In the above-mentioned example, we are trying to explain the manipulation done between the elements of the two matrices created from the two different arrays; the two matrices have been added up and printed as a result.

Calculations Across Array Elements

We can perform calculations (like addition) across the elements in an array using the apply() function.

We are performing addition on the array using apply() function.

Removal of Array elements

The elements of the array in R can be removed using %in% operator. Using this operator multiple elements can be removed at a time.

Syntax:

example[! example %in% c('x', 'y’, 'z')]

Let’s see an example of the array having character values

# Removal of array elements
x<- c('Coding','Ninjas', 'Coders', 'Programming')
# Let's remove Ninjas and Programming from the array
x <- x[! x %in% c('Ninjas', 'Programming')]
# Print the array
x

Output:

Explanation:

As you can see in the above example, both ‘Ninjas’ and ‘Programming’ are removed from the vector.

Let’s see an example of the vector having integer values.

# Removal of array elements
x<- c(1, 4, 6, 5, 7, 8, 3, 5, 9)
# Let's remove 1, 6, 5, 3 from the array
x <- x[! x %in% c(1, 6, 5, 3)]
# Print the array
x

Output:

Explanation:

As you can see every occurrence of the values ‘1’, ‘6’, ‘5’ and ‘3’ has been removed from the vector as given in the code.

R is an open-source programming language and environment for our purposes, statistical computing, and analysis or data science.

What do you mean by array data structure?

Arrays are the collection of similar data items stored at contiguous and adjacent memory locations.

How is an array created in R programming?

Array in R programming language is created using the array() function, which will take vectors as input and use the values in the dim parameter.

What are the different data structures in R programming?

Data Structures available in R programming are Vectors, Matrices, Lists, and Dataframe.

What can different operations be performed on an array in R programming language?

The operations that can be performed on the array in R programming are Naming the rows and columns, Accessing the elements, Manipulation of elements, and Calculations across array elements.

Conclusion

Here we come to the end of our article on Arrays in R Programming. In this article, we learn what an array is in R programming, how arrays are implemented, what all properties array have in R language, and at last, what all operations are performed on an array in R programming language.

Let's continue here and read more articles similar to this.