Matrix in R

In R, matrix is a two-dimensional data structure that stores values of same data type in rows and columns. Unlike vectors, that were one dimensional, matrix can hold values in rows and columns.

One can create matrix in R using the matrix() function. While creating a matrix using matrix() function, you need to specify data that is going to be used, and then specify how many rows and columns you want in the matrix.

For example, if you want a 2 x 3 matrix, having two rows and three columns, use the following command

Download Example File
matrix(data = c("a","b","c","d","e","f"),            nrow = 2,            ncol = 3            )

The output we get from the above command is following

Matrix in R and their usage

Now, if you look at the output above, you can see that data is moving by column rather than by row. By that, we mean the first column is filled by 1st two elements of data ; a and b, the second column with c and d, and so on.

However, what if we want the data to be filled by row rather than column, so that the first row has “a” “b” “c” in it? To do so, we use the byrow = TRUE parameter. This parameter determines how the data is filled into the matrix in R. When byrow is set to TRUE, it means that the data will be filled by rows.

a<- matrix(data = c("a","b","c","d","e","f"),                   nrow = 2,                   ncol = 3,                   byrow = TRUE )

The above command will now give the following desired output

columns and rows in R

Just like the vectors, matrix also has limitation that it holds only one kind of data type; character, numeric or logical data type at a time.

Next, if you want to create a matrix having numbers from 1 to 10 in sequence, with 2 rows, it can be done using the following command

num <- matrix(1:10,nrow=2)

The above command will generate the following output, a matrix having numbers from 1 to 10 in R. The sequence of numbers will be arranged in rows based on nrow=2 specification.

image-109

Similarly, we can have matrix of character data type as well. In the following command, the vector containing a mix of character strings and numbers is created. The c() function is used to combine these values into a single vector. Once the vector has been created, then we create a matrix by the given command below.

chr <- c("abc","jhon","xyz",9,"USA","He") matrix(chr, nrow=2)

The command for matrix uses chr vector for creating a matrix, by basing values on two rows.

The output we get from the above commands is shown below

image-111

The above matrix is created by specifying the rows. The matrix from the above vector can also be created by specifying columns using ncol=2 parameter. What if we want to have 4 columns instead of 2 columns? This wouldn’t be possible because data elements are 6, and they can’t be divided into 4 columns. Thus, the following command will give a warning

matrix(chr, ncol=4)

The command for matrix will be executed, but given that there are not enough data elements, there would be repetition of values or elements into the columns. The warning message in R will be shown as below, specifying that data cannot be divided into 4 columns rightly.

Screenshot-2023-08-20-133230

Combine Functions

There are other ways to combine vectors other than c() function. Let’s create three vectors, of persons having first names, last names and their ages using the following commands

f_name <- c("Stephen","Chris","Derrick") l_name <- c("Cury","Paul","Rose")  age <- c(32 , 29 , 34)

Now, if we want to combine two or more vectors, we can use the cbind and/or rbind functions, creating a new matrix in R. The cbind function combines the vectors by columns, and the rbind function combines vectors by rows.

For instance, if we want to combine first and last names of persons by columns and by rows we use the following commands respectively

cbind(f_name,l_name) rbind(f_name,l_name)

The above commands will generate following output,

using matrix in R

Similarly, if we want to check whether the vectors we created are numeric or not, we can use the following commands which will give the output whether data is numeric or not

is.numeric(f_name)
is.numeric(age)

As the f_name or first name of persons is non-numeric data, the output will be FALSE and the age is numeric data, output will be TRUE.

Accessing Data from a Matrix

We can also access data from matrix. To access data from a matrix, we specify the indices of row and column elements that we want to access. For instance, if we simply we want to access a whole matrix in R, we can use the name of matrix as following

mat

This is the matrix created earlier, which contains following data in it.

image-113

Now if we wish to access data from 1st row and 1st column, or from 1st row and 3rd column, we use the following commands

mat[1,1] mat[1,3]

The above commands will retrieve “abc” and “USA” from the column. Remember that, index of row is written first in the command to access an element, followed by the column index.

If we want to access a whole row, say 1st or 2nd row, we use the following commands

mat[1,] mat[2,]

The column space is empty, specifying that we only need data for rows. The same can be done for the columns using following command

mat[,3] 

Now, let’s say we don’t want certain columns while retrieving data. To do so, we use the minus sign with index of column and rows that we don’t want to include. The following command will only provide data with elements except 1st row and 1st column.

mat[-1,-1]

The following data will be retrieved from the above command

image-114

To access data from num vector we created earlier, that should be greater than a certain number, say 5, we use the following command

num[num > 5]

If we want to access multiple elements present in the num vector, we use the following command

num[num %in% c(3,5,9)]

The above command will extract only the elements that match 3, 5, and 9 from the vector.

Assigning Rows and Column Names in matrix in R

We can assign names to rows and columns to make it easier to refer to specific parts of matrix using their names rather than using their index numbers. The colnames() and rownames() functions are used to specify column and rows names, respectively.

To assign names to columns and rows to the mat matrix in this case, we use following commands

colnames(mat) <- c("col1","col2","col3") rownames(mat) <- c("row1","row2") mat

The following column and row names are assigned to the columns and rows in the matrix.

image-105

Just like assigning rows and column names, we can also remove the names of columns and rows in the matrix in R. To remove either columns or rows’ names from the matrix, say rows names, we use the following command

rownames(mat) <- NULL

The row names have been removed

Matrix in R

Functions in Matrix in R

There are different functions in matrix that can be used to perform various operations on matrices.

The first function is is.matrix() that is used to determine whether an object is a matrix or not. It returns a logical value; TRUE or FALSE, to indicate whether the object is matrix or not. For instance, if we want to check whether the “mat” is matrix or not, we use following command

is.matrix(mat)

The output we get is TRUE, because “mat” is actually a matrix.

To determine the dimension(number of rows and columns) of a matrix, dim() function is used. If we want to know the dimension of “mat” matrix, we use the following command

dim(mat)

This returns the following output, indicating that there are two rows and three columns.

image-106

Similarly, we can also know the number of rows and columns in a matrix using the following commands, where nrow() and ncol() functions come in handy.

nrow(mat) 
ncol(mat)

Another important function to be used in matrices is lenght() function that determines total number of observations in the column. This function can be used in the following way in a command

length(mat)

The above command returns that there are 6 number of observations in the “mat” matrix.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Tweet
Share
Share
Pin