In this post I will introduce the basics of R such as vectors and matrices. The credit for all the material in this and the future posts goes to DataCamp.

A basic concept in all programming languages (as well as Math) is a Variable. A variable is simply a name given to something that stores a value or something to which we assign a value. For instance we can assign 4 to a variable a i.e a = 4. In R, we can do this using the following command

We can then perform various mathematical operations with the variable, which I won’t go into details here.

Before moving on to vectors, we need to know about data types in R.

Data types

R works with numerous data types. The following are some of the most basic types you will across in R (as well as in other programming languages):

  • numeric: are decimal numbers like 4.5. A special type of numeric is an integer, which is a numeric without a decimal piece. Integers must be specified like 4L.
  • Logicals: are the boolean values TRUE and FALSE. Capital letters are important here; true and false are not valid.
  • Characters: are text values like “hello world”. creating a vector.

You can check the data type of a variable using class() function:

Now that we know how to create variable and check its data type. Let’s talk about vectors.

Vectors

A vector is simply a one dimensional array i.e an object to store data. We can create a vector in R using a combine function c() and assign it to a variable.

Note: a vector can only be composed of one data type, otherwise the lower ranking type will be coerced into the higher ranking type. The hierarchy for coercion is:

logical < integer < numeric < character

We can access elements (also known as vector subsetting) from vectors using []. In R, the indexing starts at 1 rather than 0 such as in Python. Let’s see some examples of these,

Matrices

Matrices are similar to vectors, except they are in 2 dimensions. We can create matrices from vectors using matrix()and by specifying the number of rows and columns. Let’s create a 2 by 2 matrix,

Notice the matrix is filled by col by default. We can specify byrow = TRUE if we want matrix to be filled by row

##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4

We can combine multiple vectors to create matrix either by column or row. Lets define some vectors to see these in action.

We can column bind these vectors together

##       apple    ibm
## [1,] 109.49 159.82
## [2,] 109.90 160.02
## [3,] 109.11 159.84
## [4,] 109.95 160.35
## [5,] 111.03 164.79

or row bind

##         [,1]   [,2]   [,3]   [,4]   [,5]
## apple 109.49 109.90 109.11 109.95 111.03
## ibm   159.82 160.02 159.84 160.35 164.79

Similar to vectors, we can also subset Matrix. We can select and/or subset vectors or matrices using [] where the first argument is always row and second is always column i.e [row, column].

##       apple    ibm microsoft
## [1,] 109.49 159.82     59.20
## [2,] 109.90 160.02     59.25
## [3,] 109.11 159.84     60.22
## [4,] 109.95 160.35     59.95
## [5,] 111.03 164.79     61.37
##     apple       ibm microsoft 
##    109.90    160.02     59.25
## [1] 159.82 160.02 159.84 160.35 164.79
##       apple    ibm microsoft
## [1,] 109.49 159.82     59.20
## [2,] 109.90 160.02     59.25
##       apple    ibm
## [1,] 109.49 159.82
## [2,] 109.90 160.02
## [3,] 109.11 159.84
## [4,] 109.95 160.35
## [5,] 111.03 164.79
##       apple    ibm
## [1,] 109.49 159.82
## [2,] 109.90 160.02

That’s all for this blog. If you liked this post or have any suggestions, please let me know.