Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find most frequently occuring string per row and its percentage

Tags:

string

r

I have a df in which every row is a question to which there are up to 7 answers (columns), including few NA's. I want to find the most frequently given answer per question and its percentage (and to return NA when there are ties). Ultimately, I wish to find questions where there is a consensus of >70% in the answers and the respective answer.

My data looks like this:

dat <- data.frame(rbind(A=c("Dog", "Dog", "Cat", "Dog","Dog", "Dog", "Cat"),
                        B=c("Dog", "Cat", "Cat", "Cat", "Cat", "Cat", "Cat"),
                        C=c("Cat", "Fox", "Fox", "Fish", "Dog", "Mouse", "Rat"),
                        D=c("Mouse", "Mouse", "Mouse", "Mouse", "Mouse", "Mouse", "Mouse"),
                        E=c("Pigeon", "Pigeon", "Seagull", "Pigeon", "Seagull", "Seagull", "Pigeon"),
                        G=c("Fox", "Fox", "Fox", NA, "Dog", "Dog", "Dog")))

I know that I can find the most common string using which.max():

dat$answer <- apply(dat,1,function(x) names(which.max(table(x))))

However, I aim to get something like this (where I can keep only final answers with a consensus of >.7)

output <- data.frame(cbind(dat[, 1:7], 
            rbind(A=c("Dog", .71),
                  B=c("Cat", .86),
                  c=c("Fox", .28),
                  D=c("Mouse", 1),
                  E=c("Pigeon", .57),
                  G=c(NA, NA))))  
colnames(output) <- c("X1", "X2", "X3", "X4", "X5", "X6", "X7", "Answer", "Perc")

All help is greatly appreciated! Cheers.

like image 943
Hamadryas Avatar asked Nov 25 '25 03:11

Hamadryas


1 Answers

You can create a Perc variable in the apply function:

dat[c("answer", "Perc")] <- 
  t(apply(dat, 1, function(x){
  c(names(sort(-table(x))[1]), max(table(x))[1] / ncol(dat))}))

      X1     X2      X3     X4      X5      X6     X7 answer Perc
A    Dog    Dog     Cat    Dog     Dog     Dog    Cat    Dog 0.71
B    Dog    Cat     Cat    Cat     Cat     Cat    Cat    Cat 0.86
C    Cat    Fox     Fox   Fish     Dog   Mouse    Rat    Fox 0.29
D  Mouse  Mouse   Mouse  Mouse   Mouse   Mouse  Mouse  Mouse    1
E Pigeon Pigeon Seagull Pigeon Seagull Seagull Pigeon Pigeon 0.57
G    Fox    Fox     Fox   <NA>     Dog     Dog    Dog    Dog 0.43

And assign NA if Perc is below a threshold:

dat[dat$Perc < .7, ]$Perc <- NA

      X1     X2      X3     X4      X5      X6     X7 answer Perc
A    Dog    Dog     Cat    Dog     Dog     Dog    Cat    Dog 0.71
B    Dog    Cat     Cat    Cat     Cat     Cat    Cat    Cat 0.86
C    Cat    Fox     Fox   Fish     Dog   Mouse    Rat    Fox <NA>
D  Mouse  Mouse   Mouse  Mouse   Mouse   Mouse  Mouse  Mouse    1
E Pigeon Pigeon Seagull Pigeon Seagull Seagull Pigeon Pigeon <NA>
G    Fox    Fox     Fox   <NA>     Dog     Dog    Dog    Dog <NA>
like image 117
Maël Avatar answered Nov 27 '25 18:11

Maël



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!