Divide Each Row of Matrix & Data Frame by Vector Elements in R (2 Examples)

 

This tutorial explains how to divide the rows of a data matrix by the elements of a vector in R.

The tutorial will contain the following information:

Let’s start right away.

 

Example 1: Divide Each Row of Matrix by Elements of Vector

This example illustrates how to divide each row of a matrix by the elements of a vector object in the R programming language.

First, we have to create an exemplifying matrix in R:

mat <- matrix(1:20, ncol = 4)                  # Create example matrix
mat                                            # Print example matrix

 

table 1 matrix divide rows matrix data frame vector r

 

The output of the previous R programming code is shown in Table 1: We have created a numeric matrix with five rows and four columns.

Next, we have to create a vector object:

vec <- c(5, 1, 8, 3)                           # Create example vector
vec                                            # Print example vector
# [1] 5 1 8 3

Our vector contains four numeric elements, i.e. the same length as the number of columns of our matrix.

Now, we can use the t() function (t stands for transpose) and the / operator to divide each row of our matrix by the elements of our vector:

mat_div <- t(t(mat) / vec)                     # Divide rows of matrix by vector
mat_div                                        # Print divided matrix

 

table 2 matrix divide rows matrix data frame vector r

 

By executing the previous syntax we have created Table 2, i.e. a new matrix object containing of the results of the divisions.

 

Example 2: Divide Each Row of Data Frame by Elements of Vector

In Example 2, I’ll explain how to perform a mathematical division of each row of a data frame by the elements of a vector.

For this, we can convert our matrix that we have created in Example 1 to the data.frame class by using the as.data.frame function.

data <- as.data.frame(mat)                     # Create example data frame
data                                           # Print example data frame

 

table 3 data frame divide rows matrix data frame vector r

 

In Table 3 it is shown that we have created a data frame containing the same values as our example matrix that we have used in Example 1.

Now, we can use basically the same code as in Example 1 to divide each row of our data frame by a vector. The only difference is that we are using the as.data.frame function once again to return the results in a data frame object as well.

Have a look at the following R code and its output:

data_div <- as.data.frame(t(t(data) / vec))    # Divide rows of data frame by vector
data_div                                       # Print divided data frame

 

table 4 data frame divide rows matrix data frame vector r

 

The output of the previous R programming syntax is shown in Table 4: A data frame containing the results of our matrix/vector division.

 

Video & Further Resources

Do you need more explanations on the content of this tutorial? Then you might want to watch the following video of my YouTube channel. I explain the R programming codes of this tutorial in the video:

 

The YouTube video will be added soon.

 

Furthermore, you might want to have a look at some of the related tutorials of my website. I have released several articles about matrix calculations already.

 

Summary: At this point you should know how to use the t() function to divide rows by a vector in R. If you have further questions, let me know in the comments section.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top