You can use the following methods to calculate the ratio between values in two columns in R:
Method 1: Use Base R
#calculate ratio between variable1 and variable2
df$ratio #calculate ratio between variable1 and variable2, rounded to 2 decimal places
df$ratio 2)
Method 2: Use dplyr
library(dplyr) #calculate ratio between variable1 and variable2 df % mutate(ratio = variable1/variable2) #calculate ratio between variable1 and variable2, rounded to 2 decimal places df % mutate(ratio = round(variable1/variable2, 2))
This tutorial explains how to use each method in practice with the following data frame that shows the total shots made and attempted by various basketball players:
#create data frame df frame(players=c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'), makes=c(4, 4, 3, 6, 7, 8, 3, 10), attempts=c(12, 7, 5, 6, 10, 12, 5, 19)) #view data frame df players makes attempts 1 A 4 12 2 B 4 7 3 C 3 5 4 D 6 6 5 E 7 10 6 F 8 12 7 G 3 5 8 H 10 19
Example 1: Calculate Ratios Using Base R
The following code shows how to calculate the ratio between the values in the makes and attempts columns using base R:
#calculate ratio between makes and attempts columns
df$ratio #view updated data frame
df
players makes attempts ratio
1 A 4 12 0.3333333
2 B 4 7 0.5714286
3 C 3 5 0.6000000
4 D 6 6 1.0000000
5 E 7 10 0.7000000
6 F 8 12 0.6666667
7 G 3 5 0.6000000
8 H 10 19 0.5263158
The ratio of makes to attempts for the first player is 4 / 12 = 0.33.
In other words, the first player made about 33% of their shot attempts.
We can interpret the ratio values for every other player in a similar manner.
We can also use the round() function to round the ratio values to a certain number of decimal places:
#calculate ratio between makes and attempts columns, rounded to 2 decimal places
df$ratio 2)
#view updated data frame
df
players makes attempts ratio
1 A 4 12 0.33
2 B 4 7 0.57
3 C 3 5 0.60
4 D 6 6 1.00
5 E 7 10 0.70
6 F 8 12 0.67
7 G 3 5 0.60
8 H 10 19 0.53
Each of the values in the ratio column are now rounded to two decimal places.
Example 2: Calculate Ratios Using dplyr
The following code shows how to calculate the ratio between the values in the makes and attempts columns using the dplyr package:
library(dplyr)
#add new column that shows ratio of makes to attempts
df %
mutate(ratio = makes/attempts)
#view updated data frame
df
players makes attempts ratio
1 A 4 12 0.3333333
2 B 4 7 0.5714286
3 C 3 5 0.6000000
4 D 6 6 1.0000000
5 E 7 10 0.7000000
6 F 8 12 0.6666667
7 G 3 5 0.6000000
8 H 10 19 0.5263158
We can also use the round() function to round the ratio values to a certain number of decimal places:
library(dplyr)
#add new column that shows ratio of makes to attempts, rounded to 2 decimal places
df %
mutate(ratio = round(makes/attempts, 2))
#view updated data frame
df
players makes attempts ratio
1 A 4 12 0.33
2 B 4 7 0.57
3 C 3 5 0.60
4 D 6 6 1.00
5 E 7 10 0.70
6 F 8 12 0.67
7 G 3 5 0.60
8 H 10 19 0.53
Each of the values in the ratio column are now rounded to two decimal places.
Notice that the base R method and the dplyr method produce the same results.
Additional Resources
The following tutorials explain how to perform other common tasks in R:
How to Filter for Unique Values Using dplyr
How to Filter by Multiple Conditions Using dplyr
How to Count Number of Occurrences in Columns in R