How to create arrays with specified dimension in R?

How to create arrays with specified dimension in R?

How to create arrays with specified dimension in R?

This recipe helps you create arrays with specified dimension in R


Recipe Objective

Array is a type of object or data structure in R-language. They are designed to store values of same data-type in multiple dimensions. For example, if you wish to create an array of dimensions (3,5,4), then it will return 4 arrays each having 3 rows and 5 columns.

This recipe demonstrates how to create an array based on a specific dimension using 2 vectors.

Step 1: Creating 2 vectors

We create 2 different sized vectors using c() function

a = c(2,4,6,8,10,12,14) b = c(3,6,9,12,15,18,21,24,27)

Step 2: Creating an array

We use array() function to create an array in R-language.

Syntax: array(data, dim, dim_names)


  1. data = vectors
  2. dim = dimension of the array (rows,columns, array_count)
  3. dim_names = names of the rows/columns specified. (optional arguement)

We now use this function to create an array with 2 rows, 3 columns and 1 array count from 2 vectors. i.e. dimension = (2,3,1)

array_ = array(c(a,b), dim = c(2,3,1)) print(array_)
, , 1

     [,1] [,2] [,3]
[1,]    2    6   10
[2,]    4    8   12

Relevant Projects

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

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.