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:
vector1 < - c( 1 , 2 , 3 )
vector2 < - c( 10 , 15 , 3 , 11 , 16 , 12 )
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:
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" )
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:
vector1 < - c( 1 , 2 , 3 )
vector2 < - c( 4 , 6 , 8 , 0 , 2 , 4 )
array1 < - array(c(vector1, vector2), dim = c( 3 , 3 , 2 ))
vector3 < - c( 3 , 2 , 1 )
vector4 < - c( 2 , 4 , 6 , 8 , 3 , 5 )
array2 < - array(c(vector3, vector4), dim = c( 3 , 3 , 2 ))
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:
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" )
result < - array(c(vector1, vector2), dim = c( 3 , 3 , 2 ),
dimnames = list (row.names, column.names, matrix.names))
print (result)
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:
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)
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
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
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
dplyr Package in R Programming
The dplyr package for R offers efficient data manipulation functions. It makes data transformation and summarization simple with concise, readable syntax. Key Features of dplyrData Frame and TibbleData frames in dplyr in R is organized tables where each column stores specific types of information, l
4 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
10 min read
Function Arguments in R Programming
Arguments are the parameters provided to a function to perform operations in a programming language. In R programming, we can use as many arguments as we want and are separated by a comma. There is no limit on the number of arguments in a function in R. In this article, we'll discuss different ways
4 min read
Named List in R Programming
A list is an object in R Language which consists of heterogeneous elements. A list can even contain matrices, data frames, or functions as its elements. The list can be created using list() function in R. Named list is also created with the same function by specifying the names of the elements to ac
4 min read
Looping over Objects in R Programming
One of the biggest issues with the âforâ loop is its memory consumption and its slowness in executing a repetitive task. When it comes to dealing with a large data set and iterating over it, a for loop is not advised. In this article we will discuss How to loop over a list in R Programming Language
4 min read