Skip to content

Data Science Tutorials

For Data Science Learners

  • Two-Way ANOVA Example in R
    How to perform One-Sample Wilcoxon Signed Rank Test in R? R
  • How to Use Bold Font in
    How to Use Bold Font in R with Examples R
  • Divide data into groups in R R
  • Ogive Graph in R
    Ogive Graph in R R
  • Cumulative Sum calculation in R
    Cumulative Sum calculation in R R
  • Random Forest Machine Learning
    Random Forest Machine Learning Introduction R
  • Change ggplot2 Theme Color in R
    Change ggplot2 Theme Color in R ggthemr Package R
  • Locate position of patterns in a character string in R R
How to Calculate Ratios in R

How to Calculate Ratios in R

Posted on November 29November 29 By Admin No Comments on 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)

R

Post navigation

Previous Post: How to do Pairwise Comparisons in R?
Next Post: How to perform TBATS Model in R

Related Posts

  • Error: Can't rename columns that don't exist
    Can’t rename columns that don’t exist R
  • Creating a Histogram of Two Variables in R R
  • R Percentage by Group Calculation
    R Percentage by Group Calculation R
  • Fisher’s exact test in R
    Fisher’s exact test in R R
  • How to create contingency tables in R
    How to create contingency tables in R? R
  • What Is the Best Way to Filter by Date in R
    What Is the Best Way to Filter by Date in R? R

Leave a Reply Cancel reply

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

  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • Top 5 Books to Learn Data Engineering
  • Mastering R Programming for Data Science: Tips and Tricks
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • sorting in r
    Sorting in r: sort, order & rank R Functions R
  • Descriptive Statistics in R R
  • Top Data Modeling Tools for 2023
    Top Data Modeling Tools for 2023 Machine Learning
  • Extract columns of data frame in R R
  • How to perform TBATS Model in R
    How to perform TBATS Model in R R
  • How to Turn Off Scientific Notation in R
    How to Turn Off Scientific Notation in R? R
  • How to Prepare a Machine Learning Interview
    How to Prepare a Machine Learning Interview? Machine Learning
  • Best AI and Machine Learning Courses
    Best AI and Machine Learning Courses Machine Learning

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme