How to create a matrix in R?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How to create a matrix in R?

How to create a matrix in R?

This recipe helps you create a matrix in R

0

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

Relevant Projects

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

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.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

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.

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

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.