How to create a matrix in R?

This recipe helps you create a matrix in R

Recipe Objective

How to create a matrix in R ? A matrix is a two-dimensional data structure, i.e., a matrix contains rows and columns. Matrices are used for performing mathematical calculations. A matrix function takes the following input values: matrix (data, nrow, ncol, byrow, dimnames). Data — is our input matrix value, nrow and ncol — are the number of rows and columns required, if byrow=TRUE , the input numbers are arranged by rows and if byrow=FALSE, then they are arranged by columns. dimnames — assigns names to rows and columns of a data frame. This recipe demonstrates how we can create a matrix of any dimension in R.

Step 1 - Create a matrix

Creating a matrix using the matrix() function. with byrow=TRUE condition.

m <- matrix(c(1:10), nrow = 4, ncol = 5, byrow = TRUE) print(m) #The output is a 4*5 matrix where the numbers are arranged by rows.
 "Output of the code " : 

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

Step 2 - Create a matrix

Creating a matrix using the matrix() function. with byrow=FASLE condition.

m <- matrix(c(1:10), nrow = 4, ncol = 5, byrow = FALSE) print(m) #The output is a 4*5 matrix where the numbers are arranged by columns.
 "Output of the code " : 
   [,1] [,2] [,3] [,4] [,5]
[1,]    1    5    9    3    7
[2,]    2    6   10    4    8
[3,]    3    7    1    5    9
[4,]    4    8    2    6   10

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

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

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.

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

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.

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

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.

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.