This lesson will try to get you familiarized with R by teaching a few simple lessons and commands. Specifcally, we will deal with groupings of numbers: vector and matrices. Vectors and matrices are the foundations of datasets in R which is why it is crucial to cover them early.
In statistis, performing operations on vectors and matrices is fundamental. You can find a simple review of vector and matrix math here. R deals easily with vectors and matrices. Below, we first create some vectors, perform some mathematical operations on them, and then do the same with matrices.
# VECTORS
a<-c(1,2,3,4,5,6) #Here we create a vector called a. The entires are separated by commas in c().
#We can perform mathematical operations on this vector.
2*a # For example, this will multiply 2 times each entry in the vector.
a+2 # This will add 2 to each entry in the vector.
b<-a+2 #As before we can create a new vector with the result of any of these operations.
b
We can also create a vector from a sequence as below.
a<-seq(1:6) #The command seq() takes here one argument, the initial and final value
#and it will generate a sequence of numbers in increments of 1.
There are other neat ways to create vectors, but this will suffice for now.
Matrices are more complicated because they take on either multiple rows or columns. By convention, they are named with a capital letter. Below we create matrix A with the help of the matrix() command. The arguments for matrix() are explained below.
# MATRICES
A<-matrix(data=c(1,2,3,4,5,6), nrow=2, ncol=3,byrow = TRUE) #the entires are separated by commas, I specify the number of rows and columns, and the order by which is fills in the data.
A
#We can also perform operations on these matrices:
A*2 # will multiply 2 times each entry in the matrix
A+2
Note that we could also create the same matrix by providing a vector as an argument.
A<-matrix(data=a, nrow=2, ncol=3,byrow = TRUE) #the entires are separated by commas, I specify the number of rows and columns, and the order by which is fills in the data.
Remember that to multiply two matrices we need them to be comforable. We then perform the ‘dot product’. You can review what that means at the link above. In any case, R can perform matrix multiplication easily. Let us define matrix B.
B<-matrix(data=c(7,8,9,10,11,12), nrow=3, ncol=2,byrow = TRUE) #the entires are separated by commas, I specify the number of rows and columns, and the order by which is fills in the data.
B
## [,1] [,2]
## [1,] 7 8
## [2,] 9 10
## [3,] 11 12
We perfom matrix multiplication with the operator %*%.
A%*%B
## [,1] [,2]
## [1,] 58 64
## [2,] 139 154
This is the correct answer, and we can likewise save this new matrix as matrix C!
C<-A%*%B
C
It is often important to extract certain values from matrices. This is known as subsetting. To get a row or column from a matrix simply name it. For a given matrix A, A[row#,] will return row# amd that row alone and A[,column#] will return column# and that column alone. Let’s see.
C[1,] #return first row
C[,2] #return second column
Another important command is diag(x=) which extracts the diagonal of the matrix defined in the argument ‘x=’.
In the next section we will explore how to deal with logicals and factors.