Creating an array in R
Below are the approaches for creating an array in R.
- Using array() function
- Using matrix() function
Creating an array Using array() function
array(data, dim = NULL, dimnames = NULL)
- data: The data elements to be arranged into the array.
- dim: An optional parameter specifying the dimensions of the array.
- dimnames: An optional parameter providing names for each dimension of the array.
The below example shows how we can create an array using array() function.
# Creating a 1-dimensional array
arr1d <- array(1:5)
# Creating a multi-dimensional array
arrMulti <- array(1:12, dim = c(3, 4))
# Print the arrays with proper spacing
cat("1-Dimensional Array:\n")
print(arr1d)
cat("\nMulti-Dimensional Array:\n")
print(arrMulti)
Output:
1-Dimensional Array:
[1] 1 2 3 4 5
Multi-Dimensional Array:
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
In this approach, we are using the array() function in R to create arrays. We start by creating a 1 dimensional array using the array(1:5) syntax, which contains elements from 1 to 5. Then, we create a multidimensional array using the array(1:12, dim = c(3, 4)), defining its dimensions as 3 rows and 4 columns. Finally, we use cat() for proper spacing and print the arrays with explanatory labels.
Creating an array Using matrix() function
matrix(data, nrow = ..., ncol = ..., byrow = FALSE, dimnames = NULL)
data
: The data to be used in the matrix. It can be a vector or a list.nrow
: Optional. The number of rows in the matrix. If not specified, it is determined from the length of the data.ncol
: Optional. The number of columns in the matrix. If not specified, it is determined from the length of the data.byrow
: Optional. A logical value indicating whether the matrix should be filled by rows (TRUE
) or by columns (FALSE
). Default isFALSE
.dimnames
: Optional. A list of length 2 containing the names for the rows and columns of the matrix. Default isNULL
.
The below example shows how we can create an array using matrix() function
# Example 1: Creating a 2x3 matrix
matrix1 <- matrix(1:6, nrow = 2, ncol = 3)
cat("Example 1 - 2x3 Matrix:\n")
print(matrix1)
# Example 2: Creating a 3x2 matrix
matrix2 <- matrix(7:12, nrow = 3, ncol = 2)
cat("\nExample 2 - 3x2 Matrix:\n")
print(matrix2)
# Example 3: Creating a 4x4 matrix with repeating values
matrix3 <- matrix(rep(10, 16), nrow = 4, ncol = 4)
cat("\nExample 3 - 4x4 Matrix with Repeating Values:\n")
print(matrix3)
# Example 4: Creating a matrix with custom row names and column names
matrix4 <- matrix(letters[1:12], nrow = 3, ncol = 4, byrow = TRUE,
dimnames = list(c("Row1", "Row2", "Row3"),
c("Col1", "Col2", "Col3", "Col4")))
cat("\nExample 4 - Matrix with Custom Row and Column Names:\n")
print(matrix4)
Output:
Example 1 - 2x3 Matrix:
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
Example 2 - 3x2 Matrix:
[,1] [,2]
[1,] 7 10
[2,] 8 11
[3,] 9 12
Example 3 - 4x4 Matrix with Repeating Values:
[,1] [,2] [,3] [,4]
[1,] 10 10 10 10
[2,] 10 10 10 10
[3,] 10 10 10 10
[4,] 10 10 10 10
Example 4 - Matrix with Custom Row and Column Names:
Col1 Col2 Col3 Col4
Row1 "a" "b" "c" "d"
Row2 "e" "f" "g" "h"
Row3 "i" "j" "k" "l"
In this approach, we are using the matrix() function in R to create arrays. Example 1 creates a 2×3 matrix with values 1 to 6, Example 2 creates a 3×2 matrix with values 7 to 12, and Example 3 creates a 4×4 matrix filled with the value 10. Example 4 demonstrates creating a matrix with custom row and column names, filled with letters from “a” to “l”, arranged in 3 rows and 4 columns.
How to create an array in R
The array is the fundamental data structure in R used to store multiple elements of the same data type. In this article, we will explore two different approaches to creating an array in R Programming Language.
Contact Us