Multiply Variable of Data Frame by Certain Value in R (Example Code)

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

Creation of Example Data

data(iris)                   # Example data
head(iris)
#   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1          5.1         3.5          1.4         0.2  setosa
# 2          4.9         3.0          1.4         0.2  setosa
# 3          4.7         3.2          1.3         0.2  setosa
# 4          4.6         3.1          1.5         0.2  setosa
# 5          5.0         3.6          1.4         0.2  setosa
# 6          5.4         3.9          1.7         0.4  setosa

Example: Multiply One Specific Variable of Data Frame by Certain Value

iris$Sepal.Length * 5        # Multiply column
#   [1] 25.5 24.5 23.5 23.0 25.0 27.0 23.0 25.0 22.0 24.5 27.0 24.0 24.0 21.5 29.0
#  [16] 28.5 27.0 25.5 28.5 25.5 27.0 25.5 23.0 25.5 24.0 25.0 25.0 26.0 26.0 23.5
#  [31] 24.0 27.0 26.0 27.5 24.5 25.0 27.5 24.5 22.0 25.5 25.0 22.5 22.0 25.0 25.5
#  [46] 24.0 25.5 23.0 26.5 25.0 35.0 32.0 34.5 27.5 32.5 28.5 31.5 24.5 33.0 26.0
#  [61] 25.0 29.5 30.0 30.5 28.0 33.5 28.0 29.0 31.0 28.0 29.5 30.5 31.5 30.5 32.0
#  [76] 33.0 34.0 33.5 30.0 28.5 27.5 27.5 29.0 30.0 27.0 30.0 33.5 31.5 28.0 27.5
#  [91] 27.5 30.5 29.0 25.0 28.0 28.5 28.5 31.0 25.5 28.5 31.5 29.0 35.5 31.5 32.5
# [106] 38.0 24.5 36.5 33.5 36.0 32.5 32.0 34.0 28.5 29.0 32.0 32.5 38.5 38.5 30.0
# [121] 34.5 28.0 38.5 31.5 33.5 36.0 31.0 30.5 32.0 36.0 37.0 39.5 32.0 31.5 30.5
# [136] 38.5 31.5 32.0 30.0 34.5 33.5 34.5 29.0 34.0 33.5 33.5 31.5 32.5 31.0 29.5

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top