How to create an array sequence given only the starting point length and the step in R

This recipe helps you create an array sequence given only the starting point length and the step in R

Recipe Objective

How to create an array sequence given only the starting point, length and the step?

An array is an object that can store multiple dimensional data in it in form of rows and columns. An array can be created using array() and passing the seq() function in the array() can help us create an array given only the starting point, length and the step. Syntax : array(seq(from, length.out, by), c()) where, from : starting point length.out : The defined length of array by : step between 2 numbers c() : vector containing the dimensions of the array This recipe demonstrates an example on creating an array sequence given only the starting point, length and the step.

Step 1 - Create a 2D array

arr1 <- array(seq(from = 5, length.out = 15, by = 2), c(5, 3)) # creating a 1D array print(arr1)

"Output of code is " : 
     [,1] [,2] [,3]
[1,]    5   15   25
[2,]    7   17   27
[3,]    9   19   29
[4,]   11   21   31
[5,]   13   23   33

Step 2 - Create a 3D array

arr2 <- array(seq(from = 5, length.out = 15, by = 2), c(5, 3 , 2)) # creating a 2D array print(arr2)

"Output of code is " : 
, , 1

     [,1] [,2] [,3]
[1,]    5   15   25
[2,]    7   17   27
[3,]    9   19   29
[4,]   11   21   31
[5,]   13   23   33

, , 2

     [,1] [,2] [,3]
[1,]    5   15   25
[2,]    7   17   27
[3,]    9   19   29
[4,]   11   21   31
[5,]   13   23   33

Step 3 - Name the matrix created

column.names <- c("COL1","COL2","COL3") row.names <- c("ROW1","ROW2","ROW3","ROW4","ROW5") matrix.names <- c("Matrix1","Matrix2") result <- array(c(arr2),dim = c(5,3,2),dimnames = list(row.names,column.names,matrix.names)) print(result)

"Output of code is " : 
, , Matrix1

     COL1 COL2 COL3
ROW1    5   15   25
ROW2    7   17   27
ROW3    9   19   29
ROW4   11   21   31
ROW5   13   23   33

, , Matrix2

     COL1 COL2 COL3
ROW1    5   15   25
ROW2    7   17   27
ROW3    9   19   29
ROW4   11   21   31
ROW5   13   23   33

{"mode":"full","isActive":false}

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Build a Text Generator Model using Amazon SageMaker
In this Deep Learning Project, you will train a Text Generator Model on Amazon Reviews Dataset using LSTM Algorithm in PyTorch and deploy it on Amazon SageMaker.

Azure Text Analytics for Medical Search Engine Deployment
Microsoft Azure Project - Use Azure text analytics cognitive service to deploy a machine learning model into Azure Databricks

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

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.

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.

MLOps Project on GCP using Kubeflow for Model Deployment
MLOps using Kubeflow on GCP - Build and deploy a deep learning model on Google Cloud Platform using Kubeflow pipelines in Python

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

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.

Recommender System Machine Learning Project for Beginners-4
Collaborative Filtering Recommender System Project - Comparison of different model based and memory based methods to build recommendation system using collaborative filtering.