Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • How to Avoid Overfitting
    How to Avoid Overfitting? Machine Learning
  • How to change the column positions in R?
    How to change the column positions in R? R
  • Descriptive statistics vs Inferential statistics
    Descriptive statistics vs Inferential statistics: Guide Statistics
  • Two-Way ANOVA Example in R
    Two-Way ANOVA Example in R-Quick Guide R
  • How to Calculate Relative Frequencies in R
    How to Calculate Relative Frequencies in R? R
  • How to perform TBATS Model in R
    How to perform TBATS Model in R R
  • Count Observations by Group in R
    Count Observations by Group in R R
  • How to Add a title to ggplot2 Plots in R
    How to Add a caption to ggplot2 Plots 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

  • A Side-by-Side Boxplot in R
    A Side-by-Side Boxplot in R: How to Do It R
  • Select the First Row by Group in R
    Select the First Row by Group in R R
  • How to Recode Values in R
    How to Recode Values in R R
  • Methods for Integrating R and Hadoop
    Methods for Integrating R and Hadoop complete Guide R
  • Remove Rows from the data frame in R
    Remove Rows from the data frame in R R
  • How to Use Mutate function in R
    How to Use Mutate function 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
  • Top 7 Skills Required to Become a Data Scientist
  • Learn Hadoop for Data Science
  • How Do Online Criminals Acquire Sensitive Data
  • Top Reasons To Learn R in 2023
  • Linear Interpolation in R-approx

Check your inbox or spam folder to confirm your subscription.

 https://www.r-bloggers.com
  • How to perform kruskal wallis test in r
    How to perform the Kruskal-Wallis test in R? R
  • OLS Regression in R
    OLS Regression in R R
  • A Side-by-Side Boxplot in R
    A Side-by-Side Boxplot in R: How to Do It R
  • Top 10 Data Visualisation Tools
    Top 10 Data Visualisation Tools Every Data Science Enthusiast Must Know Course
  • Filter Using Multiple Conditions in R
    Filter Using Multiple Conditions in R R
  • How to Add Superscripts and Subscripts to Plots in R?, The basic syntax for adding superscripts or subscripts to charts in R is as follows:
    How to Add Superscripts and Subscripts to Plots in R? R
  • Create new variables from existing variables in R
    Create new variables from existing variables 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

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme