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.

1 Create Arrays in R

Create a 3-dimensional array with character elements:

arr1 = array(c("a", "b", "c", "d", "e", "f",
               "g", "h", "i", "j", "k", "l"),
             dim = c(2, 3, 2))
arr1
, , 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:

colnames(arr1) = c("C1", "C2", "C3")
rownames(arr1) = c("R1", "R2")
arr1
, , 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:

arr3 = array(rep(NA, times = 8), dim = c(2, 2, 2))
arr3
, , 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
Summary of the Functions to Coerce or Check Arrays in R
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.

2 Check Arrays in R

Check the dimension of an array:

arr1 = array(1:8, dim = c(2, 2, 2))
arr1
, , 1

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

, , 2

     [,1] [,2]
[1,]    5    7
[2,]    6    8
dim(arr1)
[1] 2 2 2

Set or change the dimension of an array:

arr1 = array(9:16, dim = c(2, 2, 2))
arr1
, , 1

     [,1] [,2]
[1,]    9   11
[2,]   10   12

, , 2

     [,1] [,2]
[1,]   13   15
[2,]   14   16
dim(arr1) = c(2,4,1)
arr1
, , 1

     [,1] [,2] [,3] [,4]
[1,]    9   11   13   15
[2,]   10   12   14   16

Check if an object is an array:

arr1 = array(1:12, dim = c(2, 3, 2))
is.array(arr1) # This will return TRUE or FALSE.
[1] TRUE

Check the data type or mode of an array:

arr1 = array(1:12, dim = c(2, 3, 2))
typeof(arr1)
[1] "integer"
arr2 = array(c("A", "B", "C", "D",
               "A", "B", "C", "D" ),
             dim = c(2, 2, 2))
typeof(arr2)
[1] "character"

Check if an array’s components are of a type/mode:

arr1 = array(1:16, dim = c(2, 4, 2))
is.integer(arr1)
[1] TRUE
is.numeric(arr1)
[1] TRUE
arr2 = array(c("A", "B", "C", "D",
               "A", "B", "C", "D" ),
             dim = c(2, 2, 2))
is.character(arr2)
[1] TRUE

3 Coerce (into) Arrays in R

Coerce an object into an array:

This example turns a matrix into an array.

mat1 = matrix(c(1, 2, 4, 8,
                16, 32, 64, 128),
              nrow = 2, byrow = TRUE)
mat1
     [,1] [,2] [,3] [,4]
[1,]    1    2    4    8
[2,]   16   32   64  128
arr1 = as.array(mat1)
arr1
     [,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:

arr1 = array(1:16, dim = c(2, 4, 2))
arr1
, , 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
typeof(arr1)
[1] "integer"
arr2 = as.numeric(arr1)
arr2
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16
typeof(arr2)
[1] "double"
arr3 = as.character(arr1)
arr3
 [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12" "13" "14" "15"
[16] "16"
typeof(arr3)
[1] "character"

4 Add and Subtract Arrays in R

arr1 = array(c(10, 20, 30, 40,
               50, 60, 70, 80),
             dim = c(2, 2, 2))
arr1
, , 1

     [,1] [,2]
[1,]   10   30
[2,]   20   40

, , 2

     [,1] [,2]
[1,]   50   70
[2,]   60   80
arr2 = array(1:8, dim = c(2, 2, 2))
arr2
, , 1

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

, , 2

     [,1] [,2]
[1,]    5    7
[2,]    6    8
# Add two arrays:
arr1 + arr2
, , 1

     [,1] [,2]
[1,]   11   33
[2,]   22   44

, , 2

     [,1] [,2]
[1,]   55   77
[2,]   66   88
# Subtract a number from an array
arr1 - 1
, , 1

     [,1] [,2]
[1,]    9   29
[2,]   19   39

, , 2

     [,1] [,2]
[1,]   49   69
[2,]   59   79

5 Multiply and Divide Arrays in R

Element by element multiplication of arrays:

arr1 = array(c(10, 20, 30, 40,
               50, 60, 70, 80),
             dim = c(2, 2, 2))
arr1
, , 1

     [,1] [,2]
[1,]   10   30
[2,]   20   40

, , 2

     [,1] [,2]
[1,]   50   70
[2,]   60   80
arr2 = array(1:8, dim = c(2, 2, 2))
arr2
, , 1

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

, , 2

     [,1] [,2]
[1,]    5    7
[2,]    6    8
arr1 * arr2
, , 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\).

arr1 %*% arr2
     [,1]
[1,] 2040

Divide an array by a number:

arr1 / 5
, , 1

     [,1] [,2]
[1,]    2    6
[2,]    4    8

, , 2

     [,1] [,2]
[1,]   10   14
[2,]   12   16

6 Join Strings from Character Arrays in R

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" 
paste(arr1, arr2)
[1] "Letter A" "Letter B" "Letter C" "Letter D" "Letter E" "Letter F" "Letter G"
[8] "Letter H"
# paste0 will not add spacing when joining the strings
paste0(arr1, arr2)
[1] "LetterA" "LetterB" "LetterC" "LetterD" "LetterE" "LetterF" "LetterG"
[8] "LetterH"
paste("This is Letter", arr2, "in CAPITAL.")
[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."

Copyright © 2020 - 2024. All Rights Reserved by Stats Codes