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

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Learn Object Tracking (SOT, MOT) using OpenCV and Python
Get Started with Object Tracking using OpenCV and Python - Learn to implement Multiple Instance Learning Tracker (MIL) algorithm, Generic Object Tracking Using Regression Networks Tracker (GOTURN) algorithm, Kernelized Correlation Filters Tracker (KCF) algorithm, Tracking, Learning, Detection Tracker (TLD) algorithm for single and multiple object tracking from various video clips.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

Build Time Series Models for Gaussian Processes in Python
Time Series Project - A hands-on approach to Gaussian Processes for Time Series Modelling in Python

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.

Time Series Classification Project for Elevator Failure Prediction
In this Time Series Project, you will predict the failure of elevators using IoT sensor data as a time series classification machine learning problem.

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

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.

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.