Introduction to vectors and matrices in R
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:
numeric_type <- 20
character_type <- "hello"
logical_type <- FALSE
# Check which type these variables have:
class(numeric_type)
## [1] "numeric"
class(character_type)
## [1] "character"
class(logical_type)
## [1] "logical"
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.
# numeric
ibm_stock <- c(159.82, 160.02, 159.84)
ibm_stock
## [1] 159.82 160.02 159.84
# character
finance <- c("stocks", "bonds", "investments")
finance
## [1] "stocks" "bonds" "investments"
# boolean
logic <- c(TRUE, FALSE, TRUE)
logic
## [1] TRUE FALSE TRUE
# we can also check the class of each of these vectors
class(ibm_stock)
## [1] "numeric"
class(finance)
## [1] "character"
class(logic)
## [1] "logical"
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
a <- c(1L , "I am a character")
typeof(a)
## [1] "character"
b <- c(TRUE, "Hello")
typeof(b)
## [1] "character"
c <- c(FALSE, 2)
typeof(c)
## [1] "double"
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,
v <- c(1, 2, 3, 4)
v
## [1] 1 2 3 4
matrix(v, nrow = 2, ncol = 2)
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
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.
apple <- c(109.49, 109.90, 109.11, 109.95, 111.03)
ibm <- c(159.82, 160.02, 159.84, 160.35, 164.79)
microsoft <- c(59.20, 59.25, 60.22, 59.95, 61.37)
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.