Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • Best Online Course For Statistics
    Free Best Online Course For Statistics Course
  • How to apply a transformation to multiple columns in R?
    How to apply a transformation to multiple columns in R? R
  • Descriptive statistics vs Inferential statistics
    Descriptive statistics vs Inferential statistics: Guide Statistics
  • how to draw heatmap in r
    How to draw heatmap in r: Quick and Easy way R
  • How to Create a Frequency Table by Group in R
    How to Create a Frequency Table by Group in R? R
  • How Do Machine Learning Chatbots Work
    How Do Machine Learning Chatbots Work Machine Learning
  • How to Implement the Sklearn Predict Approach
    How to Implement the Sklearn Predict Approach? R
  • Cumulative Sum calculation in R
    Cumulative Sum calculation in R R
R Percentage by Group Calculation

R Percentage by Group Calculation

Posted on September 13September 13 By Jim No Comments on R Percentage by Group Calculation
Tweet
Share
Share
Pin

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.

Tweet
Share
Share
Pin
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 Calculate Ratios in R
    How to Calculate Ratios in R R
  • How to Label Outliers in Boxplots in ggplot2
    How to Label Outliers in Boxplots in ggplot2? R
  • Is R or Python Better for Data Science in Bangalore
    Is R or Python Better for Data Science in Bangalore R
  • Convert multiple columns into a single column
    Convert multiple columns into a single column-tidyr Part4 R
  • Making games in R- Nara and eventloop Game Changers
    Making games in R- Nara and eventloop Game Changers Machine Learning
  • Arrange Data by Month in R
    Arrange Data by Month in R with example 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)
  • Dynamic data visualizations in R
    Dynamic data visualizations 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
  • Error: Can't rename columns that don't exist
    Can’t rename columns that don’t exist R
  • How to Use Mutate function in R
    How to Use Mutate function in R R
  • Data Science Challenges in R Programming Language
    Data Science Challenges in R Programming Language Machine Learning
  • How Do Online Criminals Acquire Sensitive Data
    How Do Online Criminals Acquire Sensitive Data Machine Learning
  • How to test the significance of a mediation effect
    How to test the significance of a mediation effect R
  • How to Filter Rows In R
    How to Filter Rows In R? R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme