Skip to content

Data Science Tutorials

For Data Science Learners

  • Augmented Dickey-Fuller Test in R
    Augmented Dickey-Fuller Test in R R
  • Find the Maximum Value by Group in R
    Find the Maximum Value by Group in R R
  • How to choose optimal number of epochs in R
    How to choose optimal number of epochs in R Machine Learning
  • How to compare the performance of different algorithms in R
    How to compare the performance of different algorithms in R? R
  • Best Books to Learn R Programming
    Best Books to Learn R Programming Course
  • what-is-epoch-in-machine-learning
    What is Epoch in Machine Learning? Machine Learning
  • Linear Interpolation in R
    Linear Interpolation in R-approx R
  • How to Change X-Axis Labels of Barplot In R
    How to Change X-Axis Labels of Barplot In R R
R Percentage by Group Calculation

R Percentage by Group Calculation

Posted on September 13September 13 By Admin No Comments on R Percentage by Group Calculation

R Percentage by Group Calculation, The usage of this syntax in practice is demonstrated by the example that follows.

R Percentage by Group Calculation

droplevels in R with examples – Data Science Tutorials

Consider the following data frame, which displays the number of points different basketball players on different teams have scored:

Let’s create a data frame

df <- data.frame(team=c('A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B'),
points=c(112, 229, 234, 104, 100, 111, 77, 136, 134, 122))

Now we can view the data frame

df
    team points
1     A    112
2     A    229
3     A    234
4     A    104
5     A    100
6     B    111
7     B     77
8     B    136
9     B    134
10    B    122

The team percentage of all points scored can be displayed in a new column in the data frame by using the following code.

Arrange Data by Month in R with example – Data Science Tutorials

library(dplyr)

Now we can calculate the percentage of points scored, grouped by team

df1<-data.frame(df %>%
  group_by(team) %>%
  mutate(percent = points/sum(points)))
df1
    team points   percent
1     A    112 0.1437741
2     A    229 0.2939666
3     A    234 0.3003851
4     A    104 0.1335045
5     A    100 0.1283697
6     B    111 0.1913793
7     B     77 0.1327586
8     B    136 0.2344828
9     B    134 0.2310345
10    B    122 0.2103448

The percentage column displays the player’s share of the team’s total points scored.

For example, players on team A scored a total of 773 points.

As a result, the individual in the first row of the data frame, who scored 112 points, accounted for 112/773 = 14% of all the points achieved by team A.

Separate a data frame column into multiple columns-tidyr Part3 (datasciencetut.com)

And so forth.

Check your inbox or spam folder to confirm your subscription.

R

Post navigation

Previous Post: How to Find Optimal Clusters in R?
Next Post: Error in rbind(deparse.level …) numbers of columns of arguments do not match

Related Posts

  • How to Rank by Group in R?
    How to Rank by Group in R? R
  • How to convert characters from upper to lower case in R
    How to convert characters from upper to lower case in R? R
  • Multiple Plots to PDF in R R
  • How to Find Optimal Clusters in R, K-means clustering is one of the most widely used clustering techniques in machine learning.
    How to Find Optimal Clusters in R? R
  • Algorithm Classifications in Machine Learning
    Algorithm Classifications in Machine Learning Machine Learning
  • pheatmap function in R
    The pheatmap function 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.

  • How to Change X-Axis Labels of Barplot In R
    How to Change X-Axis Labels of Barplot In R R
  • Data Analytics Online Courses for Beginners
    Data Analytics Online Courses for Beginners Course
  • How to Create a Frequency Table by Group in R
    How to Create a Frequency Table by Group in R? R
  • How to create a ggalluvial plot in r
    How to create a ggalluvial plot in R? R
  • ChatGPT Coursera Review
    ChatGPT Coursera Review Course
  • Solving Systems of Equations in R R
  • Count Observations by Group in R
    Count Observations by Group in R R
  • Making games in R- Nara and eventloop Game Changers
    Making games in R- Nara and eventloop Game Changers Machine Learning

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme