ant
ant

Reputation: 585

A more intelligent way to get percentages for relative frequencies with dplyr?

Learning R as a hobby and using some of my hearthstone matches

Background: Got here using dplyr:

todayhs %.%
   group_by(hero, result) %.%
   select(hero, opponent, result) %.%
   summarise(
     count = n())

Data:

hero      result    count   
Mage      loss      12 
Mage      win       9 
Rogue     loss      3                  
Rogue     win       1                  
Warrior   loss      6                  
Warrior   win       5                  

Expected result: A column of percentages for that specific hero

hero      result    count   percent
Mage      loss      12      57%
Mage      win       9       43% 
Rogue     loss      3       75%           
Rogue     win       1       25%           
Warrior   loss      6       55%           
Warrior   win       5       45% 

My obstacle:
I understanding filter(hero = "Mage") and using prop.table will get me the result of percentages of that individual class, but is there a way to get all of the data like above at once?

My attempts

 transform(todayhs.mage, percents = ifelse(hero == "Mage",    
 prop.table(todayhs.mage$count[1:2]),""))          

Gave me

 hero      result count      percents
 Mage      loss    12        0.571428571428571
 Mage      win     9         0.428571428571429
 Rogue     loss    3                  
 Rogue     win     1                  
 Warrior   loss    6                  
 Warrior    win    5      

I assume i can write a function and individually strip them out.. but that doesn't feel right. Maybe there is a better way using dplyr adding a group_by(hero, count)? I'm scratching my head over here.

Upvotes: 2

Views: 296

Answers (2)

David Arenburg
David Arenburg

Reputation: 92302

Or using data.table (as you didn't say that it has to be a dplyr solution)

library(data.table)
setDT(todayhs)[, Percent := paste0(round(count/sum(count)*100), "%"), by = hero]

#       hero result count Percent
# 1:    Mage   loss    12     57%
# 2:    Mage    win     9     43%
# 3:   Rogue   loss     3     75%
# 4:   Rogue    win     1     25%
# 5: Warrior   loss     6     55%
# 6: Warrior    win     5     45%

Upvotes: 3

akrun
akrun

Reputation: 887531

You could try:

todayhs <- read.table(text="hero      result    count   
Mage      loss      12 
Mage      win       9 
Rogue     loss      3                  
Rogue     win       1                  
Warrior   loss      6                  
Warrior   win       5",sep="",header=T,stringsAsFactors=F)    

 library(dplyr)
 todayhs%>%
 group_by(hero)%>%
 mutate(percent=paste0(round(100*count/sum(count)),"%"))
# Source: local data frame [6 x 4]
 #Groups: hero

 #     hero result count percent
 # 1    Mage   loss    12     57%
 # 2    Mage    win     9     43%
 # 3   Rogue   loss     3     75%
 # 4   Rogue    win     1     25%
 # 5 Warrior   loss     6     55%
 # 6 Warrior    win     5     45%

Upvotes: 3

Related Questions