Open In App

How to Calculate Ratios in R

Last Updated : 01 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Ratios are used to compare quantities by dividing them. They help us to understand how one quantity is related to another quantity. In data analysis, they’re important for understanding proportions. In this article, we will learn How to Calculate Ratios in R Programming Language.

In R programming language we can find ratios by using two methods

  • Using Base R
  • Using dplyr

Calculate Ratios Using Base R

We can use the arithmetic operators in Base R, such as division (/) and the round() function, for rounding numerical values. No external packages need to be installed since these functions come by default with R Installation.

Syntax:

data_frame$ratio <- with(data_frame, column1 / column2)

Parameters:

  • data_frame$ratio: This refers to the new column named ratio within the data frame data_frame.
  • <-: The assignment operator; assigns the result of the expression on the right to the object on the left
  • with(data_frame, …): A function that evaluates an expression in the context of a data frame. In this case, it allows us to access columns column1 and column2 directly without repeatedly specifying data_frame$.
  • column1: This refers to the values in the column1 of the data_frame
  • /: The division operator, is used here to divide the values in column1 by values in column2.
  • column2: This refers to the values in the column2 of the data_frame..

Here, We create a dataset “students” with columns: “student_id”, “correct_answers”, and “total_questions_attempted”. We calculate percentages by dividing correct answers by total attempted questions, store results in a “percentage” column, and then round them to two decimal places using round(). The updated dataset is printed.

R




# Sample dataset
students <- data.frame(
  student_id = c(1, 2, 3, 4, 5),
  correct_answers = c(15, 20, 18, 25, 22),
  total_questions_attempted = c(20, 25, 22, 30, 28)
)
 
# Calculate percentage using base R
students$percentage <- students$correct_answers / students$total_questions_attempted * 100
 
# Round percentage to 2 decimal places
students$percentage <- round(students$percentage, 2)
 
# View updated dataset
print(students)


Output:

  student_id correct_answers total_questions_attempted percentage
1 1 15 20 75.00
2 2 20 25 80.00
3 3 18 22 81.82
4 4 25 30 83.33
5 5 22 28 78.57

Using dplyr

We utilize the dplyr package for its data manipulation features, including the mutate() function. This function helps us in creating new columns in data frames. The calculated ratios are along with the original data in a new column. This improves data organization and analysis. Since dplyr extends data manipulation capabilities, we need to install external packages.

mutate() Function

The mutate() function is a part of the dplyr package in the R programming language. It is a powerful data manipulation tool. The mutate() function creates new columns within your data frame. It allows you to apply various transformations and calculations to existing data.

Syntax:

data_frame <- data_frame %>%

mutate(ratio = column1 / column2)

Parameters:

  • data_frame <- data_frame: This Assigns the modified data frame back to data_frame.
  • %>%: This symbol is known as the pipe operator, which is used to forward the output of the expression on its left to the function on its right as its first argument.
  • mutate(): Function from dplyr for adding or modifying columns in a data frame.
  • ratio = column1 / column2: Creates a new column named ratio, calculated as the quotient of column1 divided by column2.

Now calculate the ratios

Here, We create a dataset “monthly_finances” containing months, revenue, and expenses. Using dplyr, we calculate profit margins by subtracting expenses from revenue and dividing by revenue. The updated dataset is then printed to display the new “profit_margin” column.

R




# Install dplyr package if you haven't already
 install.packages("dplyr")
# Sample dataset
monthly_finances <- data.frame(
  month = c("January", "February", "March", "April", "May"),
  revenue = c(50000, 55000, 60000, 58000, 62000),
  expenses = c(35000, 38000, 40000, 39000, 42000)
)
 
# Calculate profit margin using mutate() from dplyr
monthly_finances <- monthly_finances %>%
  mutate(profit_margin = (revenue - expenses) / revenue)
 
# View updated dataset
print(monthly_finances)


Output:

     month revenue expenses profit_margin
1 January 50000 35000 0.3000000
2 February 55000 38000 0.3090909
3 March 60000 40000 0.3333333
4 April 58000 39000 0.3275862
5 May 62000 42000 0.3225806

Conclusion

In conclusion, calculating ratios in R is important for comparing and interpreting quantities in data analysis. In this article, We learned two methods: base R operations and the dplyr package for Calculating Ratios in R.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads