Zaza
Zaza

Reputation: 488

calculating percentage group by group in a column R

Again I'm absolutely new in R and trying to sort a problem I faced to sort. I have a data set "ds" with column "group" and "match" I need to calculate percentage of column match group per group the first group would be 001 second group is 002, third one 003, and so on.

I have now a data :

>ds

group   col1    col2       match  
 001       F       M       FALSE   
 001       F       M       FALSE    
 001       F       M       FALSE    
 002       F       M       FALSE 
 002       M       M       TRUE    
 003       M       F       FALSE
 003       F       F       TRUE    
 003       F       F       TRUE    
 003       F       M       FALSE    
 004       F       M       FALSE 
 005       F       F       TRUE 
 005       M       F       FALSE    
 005       M       M       TRUE    
 006       M       M       TRUE         
 006       F       M       FALSE 
 006       F       M       FALSE
 006       F       M       FALSE
 006       F       M       FALSE
 006       F       F       TRUE
 006       F       M       FALSE 
 006       F       M       FALSE



Desired result:


group | col1   |  col2  |  match  | mismatch % | Match %
 001       F       M       FALSE        1.0         0    
 001       F       M       FALSE    
 001       F       M       FALSE    
 002       F       M       FALSE        0.50       0.50
 002       M       M       TRUE    
 003       M       F       FALSE        0.50       0.50
 003       F       F       TRUE    
 003       F       F       TRUE    
 003       F       M       FALSE    
 004       F       M       FALSE        1.0        0
 005       F       F       TRUE         0.66       0.33
 005       M       F       FALSE    
 005       M       M       TRUE    
 006       M       M       TRUE         
 006       F       M       FALSE        0.75       0.25
 006       F       M       FALSE
 006       F       M       FALSE
 006       F       M       FALSE
 006       F       F       TRUE
 006       F       M       FALSE 
 006       F       M       FALSE

I know how to calculate if for entire column:

percentage <- table(ds$match)
cbind(percentage,prop.table(percentage))

but can't adopt it for my solution shown higher

Thanks

Upvotes: 1

Views: 2849

Answers (1)

mfidino
mfidino

Reputation: 3055

This would be one way to do it with dplyr

We can take advantage of 2 things here: you have a column full of true/false values and the fact that the amount of mismatch you have is just 1 minus the proportion of matches.

library(dplyr)
# test data
data <- data.frame(group = factor(c(1,1,1,2,2)), col1 = c(1,1,1,1,0), col2 = c(0,0,0,0,0),
               match = c(F,F,F,F,T))

# group by group, then sum the T/F vector and divide by the number of data
# points per group
desired <- data %>% group_by(group) %>% summarise(Match_per = sum(match)/length(match))

# the mismatch is 1 minus the match
desired$Mismatch <- 1 - desired$Match

data$Match_percent <- NA
data$Mismatch_percent <- NA
data[which(duplicated(data$group) == FALSE),
which(colnames(data) %in%c("Match_percent", 
    "Mismatch_percent"))] <- desired[,=1] 

output

  group col1 col2 match Match_percent Mismatch_percent
1     1    1    0 FALSE           0.0              1.0
2     1    1    0 FALSE            NA               NA
3     1    1    0 FALSE            NA               NA
4     2    1    0 FALSE           0.5              0.5
5     2    0    0  TRUE            NA               NA

Upvotes: 3

Related Questions