Arrays in R Programming

 DATA TYPE:



Graphical Representation of Data Types in R is shown as below:

ARRAY:

An array is created using the array() function. It is Multi-Dimensional.

Syntax:

array(data = NA, dim = length(data), dimnames = NULL)

Data-             Can be a Vector
dim-               c(x,y,k); x indicates the number of rows, y indicates the number of columns, k                                        denotes the number of Matrices
dimnames-  NULL or List

1. Single Dimensional Array

a1<-array(1:10)
a1
 [1]  1  2  3  4  5  6  7  8  9 10
class(a1)
[1] "array"

2. Two Dimensional Array

a2<-array(1:10,dim=c(5,2))
a2
     [,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8
[4,]    4    9
[5,]    5   10

3. Three Dimensional Array

Let us create 2X2 matrix of 2 dimension matrix.

a3<-array(1:8,dim=c(2,2,2),dimnames=NULL)
a3
, , 1

     [,1] [,2]
[1,]    1    3
[2,]    2    4

, , 2

     [,1] [,2]
[1,]    5    7
[2,]    6    8

4. Naming Columns And Rows

Let us name the array using dimnames.

Naming Single Dimensional Array "a1"
n1<-c("E1","E2","E3","E4","E5","E6","E7","E8","E9","E10")
a1<-array(1:10,dimnames = list(n1))
a1
 E1  E2  E3  E4  E5  E6  E7  E8  E9 E10 
  1   2      3     4    5    6     7     8    9    10

Naming Two Dimensional Array "a2"
a2<-array(1:10,dim=c(5,2),dimnames = list(Rn2,Cn2))
Rn2<-c("R1","R2","R3","R4","R5")
Cn2<-c("C1","C2")
a2
   C1 C2
R1  1  6
R2  2  7
R3  3  8
R4  4  9
R5  5 10

Naming Three Dimensional Array "a3"
row_names <- c("R1", "R2") 
col_names <- c("C1", "C2") 
mat_names <- c("M1", "M2") 
a3<-array(1:8,dim=c(2,2,2),dimnames = list(row_names,col_names,mat_names))
a3
, , M1

   C1 C2
R1  1  3
R2  2  4

, , M2

   C1 C2
R1  5  7
R2  6  8

5. Accessing R Array Elements:

We can access the array using index position and name

A. Accessing Using Index Position:

Let us try accessing Single Dimensional Array "a1"
a1[2]
[1] 2

Accessing Two Dimensional Array "a2" elements.
a2[1,2] #Print Element from 1st Row 2nd Column
[1] 6

Accessing Three Dimensional Array "a3" elements.

print(a3[,1,]) # Print first column of both matrix
   M1 M2
R1  1  5
R2  2  6
print(a3[2,1,1]) # Print Second Row & 1st column of first matrix
[1] 2
print(a3[1,,2]) #Print 1st row of second matrix
C1 C2 
 5  7
print(a3[1:2,1:2,1])  #Print 1st & 2nd Row, 1st & 2nd Column from first matrix
   C1 C2
R1  1  3
R2  2  4
print (a3[, c(1, 2), 1])  #print all rows,1st and 2nd columns of matrix 1 
   C1 C2
R1  1  3
R2  2  4


B. Accessing Using Row & Column Names:

Accessing Single Dimensional Array "a1" using name.
a1["E1"]
E1 
 1

Accessing Two Dimensional Array "a2" using name.
a2["R4","C2"]
[1] 9

Accessing Three Dimensional Array "a3" using name.
a3["R2","C2","M2"]
[1] 8

6. Array Arithmetic:

A. Addition:

Addition operation will be performed with respective positions(the first row of the first matrix will be added to the first row of the second matrix and so on)

a4<-array(21:28,dim=c(2,2,2),dimnames=NULL)
a5<-array(9:16,dim=c(2,2,2),dimnames=NULL)
a4+a5

, , 1

     [,1] [,2]
[1,]   30   34
[2,]   32   36

, , 2

     [,1] [,2]
[1,]   38   42
[2,]   40   44

Let us see another example

row_names <- c("R1", "R2") 
col_names <- c("C1", "C2") 
mat_names <- c("M1", "M2") 
a4<-array(21:28,dim=c(2,2,2),dimnames=list(row_names,col_names,mat_names))
a4
, , M1

   C1 C2
R1 21 23
R2 22 24

, , M2

   C1 C2
R1 25 27
R2 26 28

Suppose we need to perform addition within the array, say R1C1 of Matrix M1 with R1C1 of Matrix M2 and so on, apply function can be used

r1<-apply(a4,c(1,2),sum)
r1
   C1 C2
R1 46 50
R2 48 52

In r1 matrix 46 is the result of 21 and 25

B. Subtraction:

Subtraction operation will be performed with respective positions as explained in the Addition Section.
a4-a5
, , M1

   C1 C2
R1 12 12
R2 12 12

, , M2

   C1 C2
R1 12 12
R2 12 12

C.Multiplication:

Multiplication operations will be performed with respective positions.

a4*a5
, , M1

    C1  C2
R1 189 253
R2 220 288

, , M2

    C1  C2
R1 325 405
R2 364 448

D. Division:

Division operation will be performed with respective positions.

a4/a5
, , M1

         C1       C2
R1 2.333333 2.090909
R2 2.200000 2.000000

, , M2

         C1   C2
R1 1.923077 1.80
R2 1.857143 1.75

7. Length:

print(length(a4))
[1] 8


Comments

Popular posts from this blog

Vector Data Type in R Programming

Variables in R Programming