We provide effective and economically affordable training courses for R and Python, Click here for more details and course registration !

When it is needed to store many elements of same type or mode into one data object in R, you can use array. Actually, vector and matrix are special types of array with one and two dimensions respectively.

  1. Array creation

Function array() can be used when a new array is being created in R. General form of this function is: NewArray <- array(input, dimensions, dimnames),

Where

input – usually a vector representing the data to be taken as elements of array,
dimensions – a numeric vector providing the dimension/shape information

dimnames – labels for axis for each dimension in the array .

In the following example, an array of dimension 3 and shape 3/4/5 is created.

> d1 <- c("N1", "N2","N3")   
> d2 <- c("M1", "M2", "M3", "M4")
> d3 <- c("L1", "L2", "L3", "L4","L5")
> #create an array of dimension(3*4*5)
> A <- array(1:60, c(3, 4, 5), dimnames = list(d1, d2, d3))
#when printed, 5 matrix of 3/4 shapes are returned.
> A
, , L1

   M1 M2 M3 M4
N1  1  4  7 10
N2  2  5  8 11
N3  3  6  9 12

, , L2

   M1 M2 M3 M4
N1 13 16 19 22
N2 14 17 20 23
N3 15 18 21 24

, , L3

   M1 M2 M3 M4
N1 25 28 31 34
N2 26 29 32 35
N3 27 30 33 36

, , L4

   M1 M2 M3 M4
N1 37 40 43 46
N2 38 41 44 47
N3 39 42 45 48

, , L5

   M1 M2 M3 M4
N1 49 52 55 58
N2 50 53 56 59
N3 51 54 57 60

2. Indexing an array

Same as for vector and matrix, an array can be indexed using the combination of bracket symbol [ ] with comma, colon and usually c(), along its every axis.

Following code block presents different examples of indexing an array in terms of indexing shapes for each axis.

#first element of first axis, all elements for the other axis 
#a matrix returned
> A[1,,]
   L1 L2 L3 L4 L5
M1  1 13 25 37 49
M2  4 16 28 40 52
M3  7 19 31 43 55
M4 10 22 34 46 58

#second element of second axis, all elements for the other axis
#a matrix returned
> A[,2,]
   L1 L2 L3 L4 L5
N1  4 16 28 40 52
N2  5 17 29 41 53
N3  6 18 30 42 54

#first element of first axis, third element of second axis, 
#all elements for third axis
#a vector returned
> A[1,3,]
L1 L2 L3 L4 L5 
 7 19 31 43 55 

#first element of first axis, second element of second axis, 
#third element of third axis
#a scalar returned
> A[1,2,3]
[1] 28

#first and second element of first axis, 
#all elements for other axis
#5 matrix of shape 2/4 returned
> A[1:2,,]
, , L1

   M1 M2 M3 M4
N1  1  4  7 10
N2  2  5  8 11

, , L2

   M1 M2 M3 M4
N1 13 16 19 22
N2 14 17 20 23

, , L3

   M1 M2 M3 M4
N1 25 28 31 34
N2 26 29 32 35

, , L4

   M1 M2 M3 M4
N1 37 40 43 46
N2 38 41 44 47

, , L5

   M1 M2 M3 M4
N1 49 52 55 58
N2 50 53 56 59

For more illustrative understanding of array in R, you can also watch videos on our YouTube channel:

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago