R Dividing One Data Frame Variable Through Another (Example Code)

On this page you’ll learn how to divide one column of a data frame through another column of this data frame in R.

Creation of Example Data

data(iris)                              # Load iris data set
head(iris)                              # Head of iris data set
#   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: Divide Sepal.Length Through Sepal.Width Variable

iris$Sepal.Length / iris$Sepal.Width    # Divide two columns
#   [1] 1.457143 1.633333 1.468750 1.483871 1.388889 1.384615 1.352941 1.470588 1.517241 1.580645 1.459459 1.411765 1.600000 1.433333 1.450000 1.295455 1.384615 1.457143 1.500000 1.342105 1.588235 1.378378
#  [23] 1.277778 1.545455 1.411765 1.666667 1.470588 1.485714 1.529412 1.468750 1.548387 1.588235 1.268293 1.309524 1.580645 1.562500 1.571429 1.361111 1.466667 1.500000 1.428571 1.956522 1.375000 1.428571
#  [45] 1.342105 1.600000 1.342105 1.437500 1.432432 1.515152 2.187500 2.000000 2.225806 2.391304 2.321429 2.035714 1.909091 2.041667 2.275862 1.925926 2.500000 1.966667 2.727273 2.103448 1.931034 2.161290
#  [67] 1.866667 2.148148 2.818182 2.240000 1.843750 2.178571 2.520000 2.178571 2.206897 2.200000 2.428571 2.233333 2.068966 2.192308 2.291667 2.291667 2.148148 2.222222 1.800000 1.764706 2.161290 2.739130
#  [89] 1.866667 2.200000 2.115385 2.033333 2.230769 2.173913 2.074074 1.900000 1.965517 2.137931 2.040000 2.035714 1.909091 2.148148 2.366667 2.172414 2.166667 2.533333 1.960000 2.517241 2.680000 2.000000
# [111] 2.031250 2.370370 2.266667 2.280000 2.071429 2.000000 2.166667 2.026316 2.961538 2.727273 2.156250 2.000000 2.750000 2.333333 2.030303 2.250000 2.214286 2.033333 2.285714 2.400000 2.642857 2.078947
# [133] 2.285714 2.250000 2.346154 2.566667 1.852941 2.064516 2.000000 2.225806 2.161290 2.225806 2.148148 2.125000 2.030303 2.233333 2.520000 2.166667 1.823529 1.966667

Further Resources & Related Tutorials

Below, you may find some additional resources on topics such as merging, loops, and dplyr:

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.
You need to agree with the terms to proceed

Menu
Top