How to create an array in R

In this recipe, we will learn what are arrays and how to create arrays in R. We will also learn how to access an element of an array through examples.

How to create an array in R?

In this tutorial, you will learn –
• What are arrays in R
• How to create an array in R
• How to access elements in an array


Explore the BERT Variants - ALBERT vs DistilBERT 


In R, arrays are data structures that can store data in multiple dimensions. It is important to note that all the elements in an array should be of the same data type. You can create an array in R with the help of the array() function. The array() function is supplied with a list of elements as well as the dimensions as needed. The function takes vectors as input and generates an array based on the number of dimensions. The syntax is as follows-

array(data, dim = (nrow, ncol, nmat), dimnames=names)

Parameters –
data -> vector with data
nrow -> number of rows
ncol -> number of columns
nmat -> number of matrices
dimnames -> name for the array (default = NULL)

Let us see an example –

Code:
#creating two vectors
vec1 <- c(4,2,3,1)
vec2 <- c(7,5,6,8,9,2 )

#creating an array with above 2 vectors as input
arr <- array(c(vec1,vec2),dim = c(3,3,2))

#printing array
print(arr)

Output:
, , 1

     [,1] [,2] [,3]
[1,]    4    1    6
[2,]    2    7    8
[3,]    3    5    9

, , 2

     [,1] [,2] [,3]
[1,]    2    3    5
[2,]    4    1    6
[3,]    2    7    8

Let us name the rows and columns –

Code:
#creating two vectors
vec1 <- c(4,2,3,1)
vec2 <- c(7,5,6,8,9,2 )
rnames <- c("R1", "R2", "R3")
cnames <- c("C1", "C2", "C3")
mnames <- c("Mat1", "Mat2")

#creating an array with above 2 vectors as input
arr <- array(c(vec1,vec2),dim = c(3,3,2), dimnames = list(rnames,cnames,mnames))

#printing array
print(arr)

Output:
, , Mat1

   C1 C2 C3
R1  4  1  6
R2  2  7  8
R3  3  5  9

, , Mat2

   C1 C2 C3
R1  2  3  5
R2  4  1  6
R3  2  7  8

Accessing elements of the array we just created –


Code:
#printing the second column of the second matrix of the array
print(arr[,2,2])

#printing the element in the 2nd row and 3rd column of the 2nd matrix
print(arr[2,3,2])

#printing the 1st Matrix
print(arr[,,1])

 
Output:
R1 R2 R3 
 3  1  7 
[1] 6
   C1 C2 C3
R1  4  1  6
R2  2  7  8
R3  3  5  9

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.