Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • how to draw heatmap in r
    How to draw heatmap in r: Quick and Easy way R
  • How to Join Multiple Data Frames in R
    How to Join Multiple Data Frames in R R
  • How to change the column positions in R?
    How to change the column positions in R? R
  • How to Calculate Lag by Group in R
    How to Calculate Lag by Group in R? R
  • Best Data Science YouTube Tutorials
    Best Data Science YouTube Tutorials Free to Learn Course
  • Top Data Science Examples You Should Know 2023
    Top Data Science Applications You Should Know 2023 Machine Learning
  • How to Use Bold Font in
    How to Use Bold Font in R with Examples R
  • Count Observations by Group in R
    Count Observations by Group 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

  • Remove Columns from a data frame
    How to Remove Columns from a data frame in R R
  • Survival Plot in R
    How to Perform a Log Rank Test in R R
  • How to Group and Summarize Data in R
    How to Group and Summarize Data in R R
  • droplevels in R with examples
    droplevels in R with examples R
  • Convert Multiple Columns to Numeric in R
    Convert Multiple Columns to Numeric in R R
  • Interactive 3d plot in R
    Interactive 3d plot in R-Quick Guide 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 Find Unmatched Records in R
    How to Find Unmatched Records in R R
  • How to Use Italic Font in R
    How to Use Italic Font in R R
  • How to Find Quartiles in R
    How to Find Quartiles 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
  • How to Rank by Group in R?
    How to Rank by Group in R? R
  • How to Perform Bootstrapping in R
    How to Perform Bootstrapping in R R
  • Convert multiple columns into a single column
    Convert multiple columns into a single column-tidyr Part4 R
  • Replace NA with Zero in R
    Replace NA with Zero in R R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme