Array Operations in R Programming
Last Updated :
22 Apr, 2020
Arrays are the R data objects which store the data in more than two dimensions. Arrays are n-dimensional data structures. For example, if we create an array of dimensions (2, 3, 3) then it creates 3 rectangular matrices each with 2 rows and 3 columns. They are homogeneous data structures.
Now, let’s see how to create arrays in R. To create an array in R you need to use the function called array(). The arguments to this array() are the set of elements in vectors and you have to pass a vector containing the dimensions of the array.
Array_NAME <- array(data, dim = (row_Size, column_Size, matrices, dimnames)
where,
data - An input vector given to the array.
matrices – Consists of multi-dimensional matrices.
row_Size – Number of row elements that an array can store.
column_Size – Number of column elements that an array can store.
dimnames – Used to change the default names of rows and columns according to the user’s preference.
Example:
Python3
# Create the vectors with different length
vector1 <- c(1, 2, 3)
vector2 <- c(10, 15, 3, 11, 16, 12)
# taking this vector as input
result <- array(c(vector1, vector2), dim = c(3, 3, 2))
print(result)
Output:
, , 1
[,1] [,2] [,3]
[1,] 1 10 11
[2,] 2 15 16
[3,] 3 3 12
, , 2
[,1] [,2] [,3]
[1,] 1 10 11
[2,] 2 15 16
[3,] 3 3 12
Operations on Arrays
Naming columns and rows
We can give names to the rows and columns using dimnames.
Example:
Python3
# Creating Vectors
vector1 <- c(1, 2, 3)
vector2 <- c(10, 15, 3, 11, 16, 12)
# Giving Names to rows and columns
column.names <- c("COLUMN1", "COLUMN2", "COLUMN3")
row.names <- c("ROW1", "ROW2", "ROW3")
matrix.names <- c("Matrix.NO1", "Matrix.NO2")
# taking this vector as input
result <- array(c(vector1, vector2), dim = c(3, 3, 2),
dimnames = list(row.names, column.names, matrix.names))
print(result)
Output:
, , Matrix.NO1
COLUMN1 COLUMN2 COLUMN3
ROW1 1 10 11
ROW2 2 15 16
ROW3 3 3 12
, , Matrix.NO2
COLUMN1 COLUMN2 COLUMN3
ROW1 1 10 11
ROW2 2 15 16
ROW3 3 3 12
Manipulating array elements
An array is made up of multiple dimensions and the operations are carried out by accessing elements.
Example:
Python3
# creating two vectors of different length
# and taking vector as input
vector1 <- c(1, 2, 3)
vector2 <- c(4, 6, 8, 0, 2, 4)
array1 <- array(c(vector1, vector2), dim = c(3, 3, 2))
# creating other array
vector3 <- c(3, 2, 1)
vector4 <- c(2, 4, 6, 8, 3, 5)
array2 <- array(c(vector3, vector4), dim = c(3, 3, 2))
# create matrices and add them
matrix1 <- array1[,,2]
matrix2 <- array2[,,2]
result <- matrix1 + matrix2
print(result)
Output:
[,1] [,2] [,3]
[1,] 4 6 8
[2,] 4 10 5
[3,] 4 14 9
Accessing Array elements
Using index position in matrix any element can be accessed easily. Also, we can alter/change the element in an array using index position.
Syntax:
Array_Name[row_position, Column_Position, Matrix_Level]
Example:
Python3
# Creating Vectors
vector1 <- c(1, 2, 3)
vector2 <- c(10, 15, 3, 11, 16, 12)
column.names <- c("COLUMN1", "COLUMN2", "COLUMN3")
row.names <- c("ROW1", "ROW2", "ROW3")
matrix.names <- c("Matrix.NO1", "Matrix.NO2")
# taking vector as input
result <- array(c(vector1, vector2), dim = c(3, 3, 2),
dimnames = list(row.names, column.names, matrix.names))
print(result)
# print third row of second matrix
print(result[3,,2])
Output:
, , Matrix.NO1
COLUMN1 COLUMN2 COLUMN3
ROW1 1 10 11
ROW2 2 15 16
ROW3 3 3 12
, , Matrix.NO2
COLUMN1 COLUMN2 COLUMN3
ROW1 1 10 11
ROW2 2 15 16
ROW3 3 3 12
COLUMN1 COLUMN2 COLUMN3
3 3 12
Calculation across array element
apply() function is used for calculations across array elements.
Syntax:
apply(x, margin, fun)
where,
x - an array.
margin - name of the dataset used.
fun - function to be applied to the elements of the array.
Example:
Python3
# create two vectors and take them as input in array
vector1 <- c(3, 2, 1)
vector2 <- c(2, 4, 6, 8, 0, 1)
new.array <- array(c(vector1, vector2), dim = c(3, 3, 2))
print(new.array)
# using apply and calculate the sum of rows in matrices
result <- apply(new.array, c(1), sum)
print(result)
Output:
, , 1
[,1] [,2] [,3]
[1,] 3 2 8
[2,] 2 4 0
[3,] 1 6 1
, , 2
[,1] [,2] [,3]
[1,] 3 2 8
[2,] 2 4 0
[3,] 1 6 1
[1] 26 12 16
Similar Reads
Operations on Lists in R Programming
Lists in R language, are the objects which comprise elements of diverse types like numbers, strings, logical values, vectors, list within a list and also matrix and function as its element. A list is generated using list() function. It is basically a generic vector that contains different objects. R
4 min read
Array vs Matrix in R Programming
The data structure is a particular way of organizing data in a computer so that it can be used effectively. The idea is to reduce the space and time complexities of different tasks. Data structures in R programming are tools for holding multiple values. The two most important data structures in R ar
3 min read
Append Operation on Vectors in R Programming
In this article, let us discuss different methods to concatenate/append values to a vector in R Programming Language. Append method in RVectors in the R Programming Language is a basic objects consisting of sequences of homogeneous elements. vector can be integer, logical, double, character, comple
2 min read
Sorting of Arrays in R Programming
Prerequisite: R â Array A vector is a uni-dimensional array, which is specified by a single dimension, length. A Vector can be created using the âc()â function. A list of values is passed to the c() function to create a vector. Sorting can be done either in ascending order or descending. There are f
5 min read
Data Munging in R Programming
Data Munging is the general technique of transforming data from unusable or erroneous form to useful form. Without a few degrees of data munging (irrespective of whether a specialized user or automated system performs it), the data can't be ready for downstream consumption. Basically the procedure o
11 min read
Correlation Matrix in R Programming
Correlation refers to the relationship between two variables, specifically the degree of linear association between them. In R, a correlation matrix represents this relationship as a range of values between -1 and 1.A value of -1 indicates a perfect negative linear relationship.A value of 1 indicate
5 min read
Data Handling in R Programming
R Programming Language is used for statistics and data analytics purposes. Importing and exporting of data is often used in all these applications of R programming. R language has the ability to read different types of files such as comma-separated values (CSV) files, text files, excel sheets and fi
5 min read
Parallel Programming In R
Parallel programming is a type of programming that involves dividing a large computational task into smaller, more manageable tasks that can be executed simultaneously. This approach can significantly speed up the execution time of complex computations and is particularly useful for data-intensive a
6 min read
Functions in R Programming
A function accepts input arguments and produces the output by executing valid R commands that are inside the function. Functions are useful when you want to perform a certain task multiple times. In R Programming Language when you are creating a function the function name and the file in which you a
8 min read
DataFrame Operations in R
DataFrames are generic data objects of R which are used to store the tabular data. Data frames are considered to be the most popular data objects in R programming because it is more comfortable to analyze the data in the tabular form. Data frames can also be taught as mattresses where each column of
9 min read