How to create an array in R
Last Updated :
16 Apr, 2024
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.
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.
R
# 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 is FALSE
.dimnames
: Optional. A list of length 2 containing the names for the rows and columns of the matrix. Default is NULL
.
The below example shows how we can create an array using matrix() function
R
# 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 2x3 matrix with values 1 to 6, Example 2 creates a 3x2 matrix with values 7 to 12, and Example 3 creates a 4x4 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.
Similar Reads
How to create a list in R
In this article, we will discuss What is a list and various methods to create a list using R Programming Language. What is a list?A list is the one-dimensional heterogeneous data i.e., which stores the data of various types such as integers, float, strings, logical values, and characters. These list
2 min read
How to Create an Array Formula in Excel?
Array formulas in excel are important tools. A single formula can perform multiple calculations and replace thousand of useful formulas. Most of us have never used array functions in our excel sheet just because we don't know how to use these formulas. Array formulas is one of the most confusing exc
10 min read
How to create a matrix in R
In this article, we will discuss What is a matrix and various methods to create a matrix by using R Programming Language. What is a matrix?A matrix is a two-dimensional data set that collects rows and columns. The matrix stores the data in rows and columns format. It is possible to access the data i
3 min read
How to Create a Covariance Matrix in R?
In this article, we will discuss how to create a Covariance Matrix in the R Programming Language. Covariance is the statistical measure that depicts the relationship between a pair of random variables that shows how the change in one variable causes changes in another variable. It is a measure of th
2 min read
How to convert CSV into array in R?
In this article, we are going to see how to convert CSV into an array in R Programming Language. If we load this file into any environment like python, R language .etc, the data will be displayed in rows and columns format, let us convert CSV file into an array. Each and every column in the CSV will
2 min read
How to Create Tables in R?
In this article, we will discuss how to create tables in R Programming Language. Method 1: Create a table from scratch We can create a table by using as.table() function, first we create a table using matrix and then assign it to this method to get the table format. Syntax: as.table(data) Example: I
2 min read
How to create dataframe in R
Dataframes are fundamental data structures in R for storing and manipulating data in tabular form. They allow you to organize data into rows and columns, similar to a spreadsheet or a database table. Creating a data frame in the R Programming Language is a simple yet essential task for data analysis
3 min read
How to Create a DataFrame with Nested Array
DataFrames are the most fundamental structures for managing and modifying data in the R Programming Language. They exhibit data in two dimensions, in rows and columns, with each column containing a distinct type of data. While traditional DataFrames are good at handling fundamental data types like n
3 min read
How to Create a Two Way Table in R?
In this article, we will create a two-way table in R programming language. A two-way table is used to display frequency for two categorical variables. The rows represent the categorical features and the column represents frequency. We can create two-way table using as.table() method. as.table() func
2 min read
Convert list to array in R
A list can be converted to array in R by calling unlist( ) function with created list as parameter. Now pass the unlist() function into array() function as parameter, and use dim attribute for specifying number of rows, columns and matrices. unlist() converts list to vector. Syntax : array( unlist(
1 min read