Thursday, April 13, 2023

R Tutorial Playlist - Matrix

 Matrices in R are just like those in mathematics; tables of numerical values, that you can do calculations with. In order to create matrix you have to call function matrix like:

m <- matrix(c(2, 7, 9, 12, 19, 6),  ncol= 2, byrow= TRUE, dimnames= list(c("I", "II"), c("1", "2")))

So, with function matrix, we are going to assign our new matrix to variable m. With first argument, we are providing actual values to our matrix. ncol stands for number of columns. With byrow set to TRUE, values from vector we have provided in our first argument, are going to be arranged by rows; so in regards that we have two columns set, our first row is going to contain values 2 and 7, second row 9 and 12 and so on... If we set byrow to FALSE, our first row would have values of 2 and 12, second 7 and 19... Optional argument dimnames accepts list as value and that list contains two vectors with textual values; firs vector contains names for rows, and second vector names for columns.

Now, in order to perform mathematical operations with two matrices, those two must be of equal number of rows and of equal number of columns. So, we are going to take our m matrix for this purpose.

m * m  or  m ^ 2 is going to produce new matrix that is going to hold every element of m matrix multiplied by itself.

m * 0.78 is going to produce new matrix with every element of matrix m multiplied my 0.78.

In order to get elements from matrix, we have to use slicing, same as with vectors and lists.

m[1, 3] in this case, we are going to select value from the first row and the third column.

Keep in mind that in R, indexing starts with 1 and with slicing in angle brackets, first we select rows and then columns.

m[c(1, 3), c(1, 4)] in this case we are going to select values from first and third row and from first and fourth column.

m[-3, -1] in this case, we are going to select all of values from m matrix except those from entire third row and entire first column.

m[, 3] if we exclude some part like in this case, we have provided empty place for rows, this means that we are going to select all of rows but only from third column.

m[2:3, 3:4] in this way, we are going to provide range of values, from second to third rows and from third to fourth column.

If we want to add additional column, we are going to use function cbind, but keep in mind that we must provide right number of elements. So if our matrix contains 3 rows, we can't add 4 elements in new column. Same rule applies with rows and in order to add additional row, we are going to use function rbind.

m <- cbind(25, 17, 92)

m <- rbind(5, 76, 13)

You noticed that we provided 3 elements with rbind function, and that is because with previous line we added one more column to our matrix, so now we must provide 3 elements in row in order for everything to work in proper way.

Now we are going to end with matrix 4 x 3, and in this case, the last row and the last column are not going to be properly named. In order to solve that, we are going to use functions.

colnames(m) <- c(".", "..", "...")

rownames(m) <- c("1", "2", "3", "4")


For video tutorial, link below:

No comments:

Post a Comment