Here, we will discuss arrays in R, how to create arrays, coerce arrays, and operations like add, subtract, multiply and divide arrays.
An array contains homogeneous data types, hence, its composition can be only one of the numeric (or double), integer, character, logical, complex or raw data types. It has 1 or more dimensions, the rows (also the first dimension) are horizontal while the columns (also the second dimension) are vertical, and from there, the higher dimensions are listed hierarchically.
Create a 3-dimensional array with character elements:
, , 1
[,1] [,2] [,3]
[1,] "a" "c" "e"
[2,] "b" "d" "f"
, , 2
[,1] [,2] [,3]
[1,] "g" "i" "k"
[2,] "h" "j" "l"
Name the rows and columns of an array:
, , 1
C1 C2 C3
R1 "a" "c" "e"
R2 "b" "d" "f"
, , 2
C1 C2 C3
R1 "g" "i" "k"
R2 "h" "j" "l"
Create a 4-dimensional array with numeric (or double) elements:
arr2 = array(c(1, 2, 4, 8,
16, 32, 64, 128,
256, 512, 1024, 2048,
4096, 8192, 16384, 32768),
dim = c(2, 2, 2, 2))
arr2
, , 1, 1
[,1] [,2]
[1,] 1 4
[2,] 2 8
, , 2, 1
[,1] [,2]
[1,] 16 64
[2,] 32 128
, , 1, 2
[,1] [,2]
[1,] 256 1024
[2,] 512 2048
, , 2, 2
[,1] [,2]
[1,] 4096 16384
[2,] 8192 32768
Create an empty array:
, , 1
[,1] [,2]
[1,] NA NA
[2,] NA NA
, , 2
[,1] [,2]
[1,] NA NA
[2,] NA NA
Assign values to (or edit) some index of an array:
First number (or set of numbers) is for the rows (horizontal), while the second number (or set of numbers) is for the columns (vertical), and from there the next number is for the next hierarchical dimension.
arr4 = array(rep(NA, times = 8), dim = c(2, 2, 2))
arr4[1,1,1] = 10
arr4[,2,1] = 100
arr4[,,2] = 1000
arr4
, , 1
[,1] [,2]
[1,] 10 100
[2,] NA 100
, , 2
[,1] [,2]
[1,] 1000 1000
[2,] 1000 1000
Function | Usage |
dim() |
Check or set an array’s dimension |
is.array() |
Check if array |
typeof() |
Check data type |
is.*type*() |
Check if array is of type |
as.array() |
Coerce into array |
as.*type*() |
Coerce array into type |
type is numeric, integer, character, logical, or complex.
See examples below.
Check the dimension of an array:
, , 1
[,1] [,2]
[1,] 1 3
[2,] 2 4
, , 2
[,1] [,2]
[1,] 5 7
[2,] 6 8
[1] 2 2 2
Set or change the dimension of an array:
, , 1
[,1] [,2]
[1,] 9 11
[2,] 10 12
, , 2
[,1] [,2]
[1,] 13 15
[2,] 14 16
, , 1
[,1] [,2] [,3] [,4]
[1,] 9 11 13 15
[2,] 10 12 14 16
Check if an object is an array:
[1] TRUE
Check the data type or mode of an array:
[1] "integer"
[1] "character"
Check if an array’s components are of a type/mode:
[1] TRUE
[1] TRUE
[1] TRUE
Coerce an object into an array:
This example turns a matrix into an array.
[,1] [,2] [,3] [,4]
[1,] 1 2 4 8
[2,] 16 32 64 128
[,1] [,2] [,3] [,4]
[1,] 1 2 4 8
[2,] 16 32 64 128
Coerce an array’s components into a different type/mode vector by columns:
, , 1
[,1] [,2] [,3] [,4]
[1,] 1 3 5 7
[2,] 2 4 6 8
, , 2
[,1] [,2] [,3] [,4]
[1,] 9 11 13 15
[2,] 10 12 14 16
[1] "integer"
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
[1] "double"
[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15"
[16] "16"
[1] "character"
, , 1
[,1] [,2]
[1,] 10 30
[2,] 20 40
, , 2
[,1] [,2]
[1,] 50 70
[2,] 60 80
, , 1
[,1] [,2]
[1,] 1 3
[2,] 2 4
, , 2
[,1] [,2]
[1,] 5 7
[2,] 6 8
, , 1
[,1] [,2]
[1,] 11 33
[2,] 22 44
, , 2
[,1] [,2]
[1,] 55 77
[2,] 66 88
, , 1
[,1] [,2]
[1,] 9 29
[2,] 19 39
, , 2
[,1] [,2]
[1,] 49 69
[2,] 59 79
Element by element multiplication of arrays:
, , 1
[,1] [,2]
[1,] 10 30
[2,] 20 40
, , 2
[,1] [,2]
[1,] 50 70
[2,] 60 80
, , 1
[,1] [,2]
[1,] 1 3
[2,] 2 4
, , 2
[,1] [,2]
[1,] 5 7
[2,] 6 8
, , 1
[,1] [,2]
[1,] 10 90
[2,] 40 160
, , 2
[,1] [,2]
[1,] 250 490
[2,] 360 640
Dot product (or inner product) of arrays:
This will return the sum of element-by-element products.
\((10*1) + (20*2) + (30*3) + (40*4) \;+\) \((50*5) + (60*6) + (70*7) + (80*8) = 2040\).
[,1]
[1,] 2040
Divide an array by a number:
, , 1
[,1] [,2]
[1,] 2 6
[2,] 4 8
, , 2
[,1] [,2]
[1,] 10 14
[2,] 12 16
For character arrays, you can use the paste()
or
paste0()
(with no spacing) to join components:
arr1 = array(rep("Letter", times = 8), dim = c(2, 2, 2))
arr2 = array(c("A", "B", "C", "D",
"E", "F", "G", "H" ),
dim = c(2, 2, 2))
arr1; arr2
, , 1
[,1] [,2]
[1,] "Letter" "Letter"
[2,] "Letter" "Letter"
, , 2
[,1] [,2]
[1,] "Letter" "Letter"
[2,] "Letter" "Letter"
, , 1
[,1] [,2]
[1,] "A" "C"
[2,] "B" "D"
, , 2
[,1] [,2]
[1,] "E" "G"
[2,] "F" "H"
[1] "Letter A" "Letter B" "Letter C" "Letter D" "Letter E" "Letter F" "Letter G"
[8] "Letter H"
[1] "LetterA" "LetterB" "LetterC" "LetterD" "LetterE" "LetterF" "LetterG"
[8] "LetterH"
[1] "This is Letter A in CAPITAL." "This is Letter B in CAPITAL."
[3] "This is Letter C in CAPITAL." "This is Letter D in CAPITAL."
[5] "This is Letter E in CAPITAL." "This is Letter F in CAPITAL."
[7] "This is Letter G in CAPITAL." "This is Letter H in CAPITAL."
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes