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:
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
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 #  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
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.
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.
- Replace NA with 0 (10 Examples for Data Frame, Vector & Column)
- Divide One Column of Data Frame Through Another
- rev R Function
- Convert Data Frame Column to Numeric
- The R Programming Language
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.