Earn 20 XP


Learning Objectives

  • Creating Vectors
  • Creating Matrices
  • Performing Some Simple Operations on Vectors & Matrices

Vector

  • A vector, in programming, is a type of array that is one-dimensional.
  • Now, what are dimensions? - Let’s take an example of a box to explain this. A box has three dimensions width, length, and depth (or height). Similarly, we work with “N” dimensional datasets in data science. “N” could be any number.

image.png

Matrix

  • Matrix is an arrangement of numbers into rows and columns

image.png

You may go through this simple explainer on Khan Academy:

https://www.khanacademy.org/math/precalculus/x9e81a4f98389efdf:matrices/x9e81a4f98389efdf:mat-intro/a/intro-to-matrices

Creating Vector in R

  • We can create a vector in R using the c or concatenate command in R.
  • See the example below:

image.png

image.png

Creating Sequence in R

  • We can generate a sequence of numbers in R as shown below:

image.png

Basic Arithmetic Operations on Vectors

image.png

Creating Matrix in R

  • We can create a matrix in R using the matrix command.
  • See the example below:

image.png

  • In the video coming up in the next slide, the tutor will discuss the following things in the hands-on tutorial:

    ▶︎ How to create vectors in R for numbers or objects using the c function or concatenate function

    ▶︎ How to use the seq function to create sequence data in R

    ▶︎ How to use the rep function in R to create a vector of repeated numbers or characters

    ▶︎ How to create a matrix using matrix function in R and use square brackets to extract certain elements from a matrix in R

    ▶︎ How to perform basic arithmetic functions on the elements of one vector, perform arithmetic functions on the corresponding elements of two vectors and perform element-wise arithmetic functions in a matrix in R Statistical software.

Create and Work with Vectors and Matrices in R