Arrays in R
Arrays in R
Introduction
An array is a homogeneous data structure used to store elements of the same data type in multiple dimensions. An array is a generalization of a matrix: while a matrix is always two-dimensional, an array can have two, three, or more dimensions.
Arrays are useful for storing and manipulating multidimensional data such as:
- Marks of students in different subjects and semesters
- Temperature readings over days, months, and years
- Pixel values of images (height × width × color channels)
- Scientific and statistical data
Characteristics of Arrays
- Homogeneous (all elements are of the same type).
- Can have two or more dimensions.
- Elements are stored column-wise.
- Elements are accessed by indices.
- Support element-wise arithmetic operations.
-
Created using the
array()function.
Syntax
array(data, dim)
where:
-
data: vector containing the elements. -
dim: dimensions of the array.
One-Dimensional Array
A <- array(c(10,20,30,40))
print(A)
Output
[1] 10 20 30 40
Two-Dimensional Array
A <- array(c(10,20,30,40,50,60), dim=c(2,3))
print(A)
Output
[,1] [,2] [,3]
[1,] 10 30 50
[2,] 20 40 60
Since R stores elements column-wise:
10 30 50
20 40 60
Three-Dimensional Array
A <- array(1:24, dim=c(3,4,2))
print(A)
Output
, , 1
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
, , 2
[,1] [,2] [,3] [,4]
[1,] 13 16 19 22
[2,] 14 17 20 23
[3,] 15 18 21 24
Determining Array Properties
class()
A <- array(1:12, dim=c(3,4))
class(A)
Output
[1] "matrix" # because it is 2-dimensional
For three-dimensional arrays:
A <- array(1:24, dim=c(3,4,2))
class(A)
Output
[1] "array"
length()
Returns total number of elements.
length(A)
Output
[1] 24
dim()
Returns dimensions.
dim(A)
Output
[1] 3 4 2
Accessing Elements
Consider
A <- array(1:12, dim=c(3,4))
Output
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
Access Individual Elements
A[2,3]
Output
[1] 8
(Row 2, Column 3)
Access Entire Row
A[2,]
Output
[1] 2 5 8 11
Access Entire Column
A[,3]
Output
[1] 7 8 9
Access Elements of a 3D Array
A <- array(1:24, dim=c(3,4,2))
Access:
A[2,3,1]
Output
[1] 8
(Row 2, Column 3, Layer 1)
Naming Dimensions
A <- array(
1:12,
dim=c(2,3,2),
dimnames=list(
Row=c("R1","R2"),
Col=c("C1","C2","C3"),
Layer=c("L1","L2")
)
)
print(A)
Array Arithmetic
A <- array(1:6, dim=c(2,3))
B <- array(7:12, dim=c(2,3))
print(A+B)
Output
[,1] [,2] [,3]
[1,] 8 12 16
[2,] 10 14 18
Subtraction
A-B
Multiplication
A*B
Element-wise multiplication.
Scalar Operations
A <- array(1:6, dim=c(2,3))
print(A+10)
Output
[,1] [,2] [,3]
[1,] 11 13 15
[2,] 12 14 16
Summation
sum(A)
Output
[1] 21
Mean
mean(A)
Output
[1] 3.5
Maximum and Minimum
max(A)
min(A)
Output
[1] 6
[1] 1
Applying Functions Along Dimensions
Row Sums
A <- array(1:12, dim=c(3,4))
apply(A,1,sum)
Output
[1] 22 26 30
Column Sums
apply(A,2,sum)
Output
[1] 6 15 24 33
Modifying Elements
A <- array(1:6, dim=c(2,3))
A[2,3] <- 100
print(A)
Output
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 100
Difference Between Vector, Matrix and Array
| Structure | Dimensions |
|---|---|
| Vector | One-dimensional |
| Matrix | Two-dimensional |
| Array | Two or more dimensions |
Example:
Vector
v <- c(1,2,3,4)
Matrix
M <- matrix(1:6, nrow=2)
Array
A <- array(1:24, dim=c(2,3,4))
Example: Monthly Sales Data
Suppose sales of 3 products are recorded for 4 quarters during 2 years.
sales <- array(
1:24,
dim=c(3,4,2),
dimnames=list(
Product=c("P1","P2","P3"),
Quarter=c("Q1","Q2","Q3","Q4"),
Year=c("2024","2025")
)
)
print(sales)
Find total sales:
sum(sales)
Find average sales:
mean(sales)
Find sales in 2025:
sales[,,2]
Useful Functions
| Function | Purpose |
|---|---|
array() | Create array |
dim() | Dimensions |
length() | Number of elements |
class() | Class of array |
sum() | Sum |
mean() | Average |
max() | Maximum |
min() | Minimum |
apply() | Apply function |
dimnames() | Dimension names |
Applications of Arrays
- Image processing
- Machine learning datasets
- Scientific computing
- Weather forecasting
- Financial data analysis
- Time-series data
- Multidimensional statistical data
Conclusion
Arrays are multidimensional homogeneous data structures that extend vectors and matrices to higher dimensions. They provide an efficient way to organize and manipulate multidimensional data and support powerful element-wise and statistical operations. Arrays are widely used in scientific computing, machine learning, and data analysis.
Comments
Post a Comment