A matrix is a table of numbers. In math text, it is conventional to denote matrices with bold letters. For example, consider a matrix D of the prices of the securities on first three days of the week.
1D =
2 64 31
3 65 28
4 66 35
5This matrix is {3\*2} matrix (pronounced "3 by 2") . The number of rows is given first, followed by the number of columns. The Matrix D shows that on the first day, the bond was worth $64 and the stock was worth $31. On the second day the bond was worth $65 and the stock $28. On the third day the bond was worth $66 and the stock $35.
With this data in place, we can answer many analytics questions considering someone was holding these assets in their portfolio. Let's learn how to use matrices in R and then how to perform statistical analysis on them.
Let's say we have the above data in the form of a vector:
> price_data <- c(64,31,65,28,66,35)
We know that this data represents three rows with each row containing the bond price and the stock price. In R, we can use this vector to create a matrix using the matrix() function as shown below:
1#price data each number pair represents the bond an dstock price
2price_data <- c(64,31,65,28,66,35)
3#Create matrix of prices
price_matrix
Matrix function parameters:
nrow indicates that the matrix will have three rows. A similar argument ncol could be used to indicate the number of columns.byrow indicates how the data should be processed. byrow=TRUE indicates that it should be processed by row, i.e., as data comes in it will first fill the first row, then second and so on. To process data by columns, byrow should be set to FALSE.When this matrix is printed, it will look as follows:
1> price_matrix
2 [,1] [,2]
3[1,] 64 31
4Currently our matrix doesn't have any names for the rows and columns and it's difficult to understand the data. Our first column represents the prices of the bond and the second column represents the prices of stock. The three rows represent three days, namely, Mon, Tue and Wed.
In R, we can assign names to the matrix using the rownames() and colnames() functions as shown below:
1#set row names and column names for price_matrix
2rownames(price_matrix) <- c("Mon","Tue", "Wed")
3colnames(price_matrix) <price_matrix
We can now print the matrix:
1> price_matrix
2 Bond Stock
3Mon 64 31
4Tue 65 28
5Wed 66 35
6Another easy way to assign names to a matrix is to use the dimnames() function, as shown below. Note the use of list() function which we will discuss in the upcoming lessons. This list has two objects, the first is the vector for row names and the second is the vector for column names.
1#Use the dimnames function to assign names to matrix elements
2dimnames(price_matrix) <-list(c("Mon","Tue", "Wed"),c("Bond","Stock"price_matrix
We can select specific elements of a matrix by using the expression D[m, n], i.e., mth row and nth column of matrix D.
Below R script shows some different ways of selecting the matrix elements:
1> #print the matrix
2> price_matrix
3 Bond Stock
4Mon 64 31
5Tue 65 28
6Mon Tue Wed
Bond Stock
Bond Stock
Tue Wed
Just like vectors, we can use standard operators +, -, /, * with matrices. Let's take a few examples to understand the matrix arithmetic and also understand a few other matrix operations along the way.
price_matrix to get the dollar holding value of your assets. We store this in the matrix portfolio_value.rowSums() function. We store the results in a vector called days_total.portfolio_value matrix. We can add the days_total vector to the main matrix using cbind() function.colMeans() function to calculate the column means. We store these values in a vector called days_average.days_average vector as a new row to our main matrix. Since we are adding a row, we will use rbind() function.1> #print the matrix
2> price_matrix
3 Bond Stock
4Mon 64 31
5Tue 65 28
6Mon Tue Wed
Bond Stock days_total
In the above example, we assumed the same quantity 5 for both stock and bond and simply multiplied it with the matrix. R took care of multiplying each matrix element with 5 to get us the values. However, what if we have different quantities of stock and bond. In such a case we can store the quantities in a new vector and then do standard matrix multiplication to achieve our results. Note that what we did earlier (multiply by *) is not the standard matrix multiplication for which you should use %*% in R. The calculation and nuances are demonstrated below:
1#print the matrix
2price_matrix
3# Assign quantities - 5 bonds and 3 stocks
4quantities <-c(5,3)
5# Multiply prices with quantities to get the values.%*% represents standard matrix multiplication.diag() create a diagnal matrix from the vector so that we can then multiply the two matrices.The resulting matrix will look as follows:
1> portfolio_value
2 [,1] [,2]
3Mon 320 93
4Tue 325 84
When you create a new vector, matrix or any other R object, it gets saved into the workspace and is available for you to use in your calculations. These variables can be seen in the Global Environment, i.e., the top-right window in RStudio. You can also access all objects available in the workspace using the ls() command in R console.

Using ls() command:
1> ls()
2[1] "days_average" "days_total" "final_matrix"
3[4] "portfolio_value" "portfolio_value_totals"