Multiply Column of Data Frame by Number in R (Example)

 

On this page you’ll learn how to multiply the variable of a data frame by a particular number in R.

The page will contain the following content blocks:

Let’s jump right to the R syntax:

 

Example Data

We’ll use the following data as basement for this R programming tutorial:

data <- data.frame(x1 = 1:5,      # Create example data
                   x2 = 11:15)
data                              # Print example data

 

table 1 data frame multiply column data frame number r

 

As you can see based on Table 1, our example data is a data frame made of five rows and the two variables “x1” and “x2”.

 

Example: Multiply Single Variable of Data Frame by 100

This example illustrates how to multiply a particular variable in a data frame by a certain number (i.e. 100).

For this task, we can use the $ and * operators as shown below. The $ operator extracts the values of the column that we want to multiply, and the * operator telly R that we want to multiply the values before the * sign by the values after the * sign.

Have a look at the following code and its output:

data$x1 * 100                     # Multiply data frame column
# [1] 100 200 300 400 500

As you can see, the previous R code has returned the result of our multiplied column.

We can also store these values in a new column in our data set:

data$x1_multi <- data$x1 * 100    # Create new variable
data                              # Print updated data

 

table 2 data frame multiply column data frame number r

 

The output of the previous R programming syntax is shown in Table 2: An updated data frame containing one variable with the multiplied values.

 

Video, Further Resources & Summary

If you need more explanations on the R codes of this tutorial, I recommend watching the following video on my YouTube channel. I’m explaining the R codes of this article in the video.

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Furthermore, you might read some of the other R programming articles that I have published on www.statisticsglobe.com. You can find some posts below.

 

To summarize: In this article you have learned how to multiply the column of a data frame by a particular number in R programming. If you have further questions, tell me about it in the comments.

 

Subscribe to the Statistics Globe 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.

Top