My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
Matrices in R With The matrix() Function

Matrices in R With The matrix() Function

Kush's photo
Kush
·Sep 1, 2021·

7 min read

Hello and welcome to my blog, or you can say documentation and my learning journey with R language

To introduce myself call me Kush, aspiring in Data Science from India, and here I am documenting my journey with R Language.

In this blog, or notebook, or documentation I shall cover Matrices in R using matrix() function and we shall see a few of its examples and how they are applied using R-Studio IDE. I shall give a small intro to R Language before I proceed with Matrices as I am writing this blog with the intention to reach an absolute beginner who has no knowledge about programming languages and wish to join the learning path with R just the way I did. Let's see how far my attempt can reach.

So without wasting a second let's begin.

A Small Introduction to R Language

R Programing language is a language mostly used for Statistical, and Data Analytics operations, as an open-source language and analytical it has gained high popularity in 2021, and to be noted R professionals are amongst the most highly paid professionals in Data and Analytics industry.

History of R Language comes from the year 1991 when Ross Ihaka and Robert Gentleman began implementation of S language at the University of Auckland, New Zealand, so you don't have to guess

R is an implementation of S language and the name R comes from the two Authors of language.

For your detailed Knowledge, you can check the link below that will take you to the Wikipedia page of R Programing Language as I won't be getting much into the History.

Click Here for More Details On History Of R Programing Language

So It is time, to begin with, Matrices and If you wish To follow along with this document you must have R and R-Studio installed in your Computer or Laptop hard drives as per your operating system. Below are the links through which you can download and install R and R-Studio on your device.

Install

  • R-Studio Note: Download free version for

    learning.

So If You are Installed Let's get started with Matrices with matrix() Function.

Matrices in R

What are Matrices in R?

Go by any source on the internet you will find almost the same definition "Matrices are the R objects in which elements are arranged in a two-dimensional layout, They contain elements of the same atomic types that are Numeric, Character, or Logical we create a matrix of these three elements in R amongst three we use matrices mostly for numeric elements so we shall see an example based on Numeric types to develop a better understanding.

we shall start with a vector k

k <- matrix(c(1,2,3,4,5,6), nrow = 2, ncol = 3)

Always remember in R we indicate the direction of the vector using symbol <-

What are Vectors?

Simply remember "Vectors is the sequence of elements of the same type Numeric, Character or Logical". The most common way of writing vectors in R is using the c() function.

  • Characters are also known as strings in programing language.
  • Logical values are of two types TRUE and FALSE

few essentials to remember before we proceed as this documentation is just an introduction.

we will come across

  • c() Function vectors in R are written with c() function
  • class() prints class of vectors
  • typeof() prints type of vectors
  • length() prints length of vectors
  • COLON Operators (:) helps us in creating sequences of integers, sequences of whole numbers
  • rep() we create vectors with replicated values
  • seq() creates sequences of real numbers

some essentials we must know before we proceed

Ok, Let's not deviate from our topic as I'll be coming up with more documentation of my journey in the coming days.

So in example k <- matrix(c(1,2,3,4,5,6), nrow = 2, ncol = 3), k is the vector and To create a matrix we must indicate the elements as well as number of rows using the (nrow) parameter and number of columns using (ncol) parameter. notice here (c(1,2,3,4,5,6), nrow = 2, ncol = 3) elements must be put in a container, number of rows are 2 and number of column 3. now lets print this in R-studio.

Check Image below

Matrix 1.png

to execute any command in R-Studio press ctrl + enter after your code or press run above code editor tab


k <- matrix(c(1,2,3,4,5,6), nrow = 2, ncol = 3) #(ctrl + enter)



k  #(ctrl + enter)

as the image shows by default, any matric is created column-wise so the components are arranged in column order as.

1 3 5

2 4 6

by column.

In order to create matrix row-wise, we will have to set the option to TRUE in byrow as shown in the image and example below

Code snippet (v is the vector in which values are stored)

v <- matrix(c(1,2,3,4,5,6), nrow = 2, ncol = 3, byrow = TRUE)

row wise.png

as you can see it is now arranged row-wise.

1 2 3

4 5 6

It is not necessary to specify both the number of rows and columns we can only indicate one and the other one will be computed automatically by the program but the number of elements must be a multiple of rows or columns.

In this example, we shall create the same matrix with the same components indicating only the number of rows.

b <- matrix(c(1,2,3,4,5,6), nrow=2)

Mentionig just row.png

here the number of rows is mentioned and the result in vector b is

1 3 5

2 4 6

Now let's see the same with the column.

b <- matrix(c(1,2,3,4,5,6), ncol=3)

colwise.png

this proves using either one gives us the same output.

1 3 5

2 4 6

Now let us see the same example with seven elements.

b <- matrix(c(1,2,3,4,5,6,7), nrow=2)

Same example with 7 elements.png

It shows an error because 7 is not a multiple of 2

Now let's see the class and type of object 'b'.

class and type.png

as you can see Class is a matrix because our object is a matrix and type is double because our object contains a real number.

Now let's get the dimensions of our matrix the dim() function will return a number of rows and columns of our matrix.

dim function.png

The nrow() function returns the number of rows

nrow function.png

Lastly ncol() function returns columns.

ncol function.png

Other examples of the matrix.

let us create a matrix using colon operator and row-wise

Code Snippet

s <- matrix(1:12, nrow = 4, byrow = TRUE)

mat row.png

this creates integers from 1 to 12 in a four-by-three matrix.

Now let's check the typeof()

Type obviously will be integers.png

Type is obviously integers.

Similarly, let us create a 3 by 3 matrix that contains only one value 8

3 by 3.png

s <- matrix(rep(8, 9), nrow = 3) in this code we replicated value 8 nine times then we specified the number of rows one time that is 3. it is a quadratic matrix 3 by 3

Now let's create a matrix of 20 numbers of 1 to 5 and define the elements using seq() function the number of rows is 5 and the matrix will be built a row-wise number of columns will be 4 20diveded by 5

mat1234.png

The first element in the matrix is 1 last element is 5 and in-between we have elements of the sequence.

Lastly, let us create a matrix of 20 randomly distributed numbers lets define them using the norm function

so there will be a normal distribution. with the mean of zero and standard deviation of 1 number of rows will be again 5

so we are going to get a 5 by 4 matrix of random numbers.

s <- matrix(rnorm(20), nrow = 5, byrow = TRUE)

mat row.png

Using the same exact procedure we can create matrices containing Character, or Logical Elements. The operations are the same.

Thank you for reading this far my journey in R Language hope you have an amazing day see you soon with new documentation. Until then Happy Learning and Happy Reading. Don't forget to share if you've liked it. Any suggestions I am open to improvement. Goodbye for now see you soon.