Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • How to create Sankey plot in R
    How to create a Sankey plot in R? R
  • pheatmap function in R
    The pheatmap function in R R
  • Credit Card Fraud detection in R
    Credit Card Fraud Detection in R R
  • How Do Machine Learning Chatbots Work
    How Do Machine Learning Chatbots Work Machine Learning
  • gganatogram Plot in R
    How to create Anatogram plot in R R
  • How to Use Mutate function in R
    How to Use Mutate function in R R
  • Descriptive statistics vs Inferential statistics
    Descriptive statistics vs Inferential statistics: Guide Statistics
  • Create new variables from existing variables in R
    Create new variables from existing variables in R R
How to Calculate Ratios in R

How to Calculate Ratios in R

Posted on November 29November 29 By Jim No Comments on How to Calculate Ratios in R
Tweet
Share
Share
Pin

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)

Tweet
Share
Share
Pin
R

Post navigation

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

Related Posts

  • How to Label Outliers in Boxplots in ggplot2
    How to Label Outliers in Boxplots in ggplot2? R
  • How to perform MANOVA test in R
    How to perform the MANOVA test in R? R
  • How to Scale Only Numeric Columns in R
    How to Scale Only Numeric Columns in R R
  • Bind together two data frames by their rows or columns in R
    Bind together two data frames by their rows or columns in R R
  • Add Significance Level and Stars to Plot in R
    Add Significance Level and Stars to Plot in R R
  • How to Join Data Frames for different column names in R
    How to Join Data Frames for different column names in R R

Leave a Reply Cancel reply

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

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Tips for Data Scientist Interview Openings
  • What is Epoch in Machine Learning?
  • Dynamic data visualizations in R
  • How Do Machine Learning Chatbots Work
  • Convex optimization role in machine learning

Check your inbox or spam folder to confirm your subscription.

  • Sampling from the population in R
  • Two of the Best Online Data Science Courses for 2023
  • Process of Machine Learning Optimisation?
  • ggplot2 scale in R (grammar for graphics)
  • ggplot aesthetics in R (Grammer of graphics)
  • How To Become a Business Intelligence Analyst
    How To Become a Business Intelligence Analyst Course
  • Data Science Challenges in R Programming Language
    Data Science Challenges in R Programming Language Machine Learning
  • How to Replace String in Column in R
    How to Replace String in Column using R R
  • How to use image function in R
    How to use the image function in R R
  • Add new calculated variables to a data frame and drop all existing variables
    Add new calculated variables to a data frame and drop all existing variables R
  • 5 Free Books to Learn Statistics For Data Science
    5 Free Books to Learn Statistics For Data Science Course
  • Beginner's Guide to Data Science
    Beginner’s Guide to Data Science Machine Learning
  • Two-Way ANOVA Example in R
    How to perform a one-sample t-test in R? R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme