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)

where:

  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

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

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

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

GCP MLOps Project to Deploy ARIMA Model using uWSGI Flask
Build an end-to-end MLOps Pipeline to deploy a Time Series ARIMA Model on GCP using uWSGI and Flask