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

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

Build a Speech-Text Transcriptor with Nvidia Quartznet Model
In this Deep Learning Project, you will leverage transfer learning from Nvidia QuartzNet pre-trained models to develop a speech-to-text transcriptor.

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

Linear Regression Model Project in Python for Beginners Part 2
Machine Learning Linear Regression Project for Beginners in Python to Build a Multiple Linear Regression Model on Soccer Player Dataset.

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Azure Deep Learning-Deploy RNN CNN models for TimeSeries
In this Azure MLOps Project, you will learn to perform docker-based deployment of RNN and CNN Models for Time Series Forecasting on Azure Cloud.

End-to-End Snowflake Healthcare Analytics Project on AWS-1
In this Snowflake Healthcare Analytics Project, you will leverage Snowflake on AWS to predict patient length of stay (LOS) in hospitals. The prediction of LOS can help in efficient resource allocation, lower the risk of staff/visitor infections, and improve overall hospital functioning.

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT