How To Create a Matrix in R programming
--
Matrices are a part of the data science landscape. They are essential for all types of data models, from simple tables to matrices for a Markov chain. Creating a matrix is pretty easy in R programming. This post covers the basics of matrices in R, and can help enhance your understanding of data models.
The Way R Views Matrices
Matrices are central in R programming, mainly for how the programming is designed to store each value of a matrix element.
R programming provides a convenient computing environment for matrices because of how R treats vectors. R considers vectors as its basic data object. It evaluates the objects as an interpreted language — directly executing what it sees in the objects. R stores vector elements in a column-wise arrangement. This means as it stores each value of a vector element, it arranges each one consecutively in columns. This is called column-major order (This Wikipedia page explains the details, as well as a row-major order). The major order principle is meant to organize the elements for memory access. R treats all objects in a column-major order. Scalars and matrices are variations of a vector object. In a programming script, matrices are a series of vectors tied together.
Because of this programming structure, data analysts and developers plan R functions and packages to quickly process arrays according to major order sequencing. This helps to calculate data rapidly in advanced models like those for machine learning.
Three ways to create a matrix
A matrix can be made in one of three ways within R programming. The first two are basically either combining the rows or columns using the rbind and cbind function.
In the example, there are two vectors bound together with the cbind function. cbind treats each as a column of the object matrix_col. There are 8 rows, 2 columns.