user14268322
user14268322

Reputation: 49

How to calculate a percentage of one variable based on the frequency of another in R

I am not exactly sure how to write it out, but I will show the code of what I want. I am trying to determine the accuracy of certain people in a task that was conducted on numerous occurrences, however, there is a very large number of cases.

names <- c("James", "James", "James", "James", "James", "John", "John", "Fred")
outcome <- c("successful", "unsuccessful", "unsuccessful", "successful", "successful", "successful",
         "unsuccessful", "unsuccessful")
accuracy <- c("60%", "60%", "60%", "60%", "60%", "50%", "50%", "0%")
df <- data.frame(names, outcome, accuracy)

In the above example, I have obviously manually input the data, but I was wondering how to write a code that looks at the frequency of successful/unsuccessful outcomes in relation to the person's name, and then print the percentage of the total instances that were successful in the accuracy column.

I am not really sure where to start with this, and hopefully it is a simple solution that I just didn't think of!

Thanks in advance

Upvotes: 2

Views: 72

Answers (2)

Karthik S
Karthik S

Reputation: 11596

Does this work?

> df %>% group_by(names) %>% mutate(accuracy = paste0(100 * sum(outcome == 'successful')/n(),'%'))
# A tibble: 8 x 3
# Groups:   names [3]
  names outcome      accuracy
  <chr> <chr>        <chr>   
1 James successful   60%     
2 James unsuccessful 60%     
3 James unsuccessful 60%     
4 James successful   60%     
5 James successful   60%     
6 John  successful   50%     
7 John  unsuccessful 50%     
8 Fred  unsuccessful 0%      

Upvotes: 2

jay.sf
jay.sf

Reputation: 73352

Using ave.

df$accuracy <- NULL

df <- transform(df, accuracy=ave(outcome %in% "successful", names, 
                                 FUN=function(x) paste0(sum(x)/length(x)*100, "%")))
df
#   names      outcome accuracy
# 1 James   successful      60%
# 2 James unsuccessful      60%
# 3 James unsuccessful      60%
# 4 James   successful      60%
# 5 James   successful      60%
# 6  John   successful      50%
# 7  John unsuccessful      50%
# 8  Fred unsuccessful       0%

Upvotes: 2

Related Questions