How to Calculate Ratios in R

How to Calculate Ratios in R? The following two techniques can be used in R to determine the ratio of values in two columns.

The following data frame, which displays the total number of shots taken and attempted by different basketball players, is used to demonstrate how each strategy should be used in practice.

Two-Way ANOVA Example in R-Quick Guide – Data Science Tutorials

How to Calculate Ratios in R

Let’s create a data frame

df <- data.frame(players=c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'),
                 makes=c(14, 11, 10, 12, 15, 11, 5, 8),
                 attempts=c(20, 17, 11, 13, 20, 12, 15, 19))

Now we can view the data frame

df
    players makes attempts
1       A    14       20
2       B    11       17
3       C    10       11
4       D    12       13
5       E    15       20
6       F    11       12
7       G     5       15
8       H     8       19

Example 1: Calculate Ratios Using Base R

The ratio between the values in the makes and attempts columns can be calculated using base R by running the following code:

df$ratio <- df$makes/df$attempts
df
  players makes attempts     ratio
1       A    14       20 0.7000000
2       B    11       17 0.6470588
3       C    10       11 0.9090909
4       D    12       13 0.9230769
5       E    15       20 0.7500000
6       F    11       12 0.9166667
7       G     5       15 0.3333333
8       H     8       19 0.4210526

Additionally, we can round the ratio values to a specified number of decimal places using the round() function.

ggpairs in R – Data Science Tutorials

df$ratio <- round(df$makes/df$attempts, 2)
df
players makes attempts ratio
1       A    14       20  0.70
2       B    11       17  0.65
3       C    10       11  0.91
4       D    12       13  0.92
5       E    15       20  0.75
6       F    11       12  0.92
7       G     5       15  0.33
8       H     8       19  0.42

Player D and F shows highest ratio.

Example 2: Calculate Ratios Using dplyr

Using the dplyr package, the following code illustrates how to determine the ratio between the values in the makes and attempts columns:

library(dplyr)
df <- df %>%
        mutate(ratio = makes/attempts)
df
players makes attempts     ratio
1       A    14       20 0.7000000
2       B    11       17 0.6470588
3       C    10       11 0.9090909
4       D    12       13 0.9230769
5       E    15       20 0.7500000
6       F    11       12 0.9166667
7       G     5       15 0.3333333
8       H     8       19 0.4210526

Additionally, we can round the ratio values to a specified number of decimal places using the round() function.

How to put margins on tables or arrays in R? (datasciencetut.com)

df <- df %>%
        mutate(ratio = round(makes/attempts, 2))
df
   players makes attempts ratio
1       A    14       20  0.70
2       B    11       17  0.65
3       C    10       11  0.91
4       D    12       13  0.92
5       E    15       20  0.75
6       F    11       12  0.92
7       G     5       15  0.33
8       H     8       19  0.42

The ratio column’s values have all been rounded to two decimal places at this point.

Note that the outcomes of the dplyr method and the base R method are identical.

Boosting in Machine Learning:-A Brief Overview (datasciencetut.com)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

three + 16 =