R - select the number that appears most in the line

I have df test

:

A   B   C
1   1   NA
2   NA  NA
1   2   2

      

I want to create another column, say test$D

which is the number that appears the most on that row, except for NA. My desired df:

A   B   C   D
1   1   NA  1
2   NA  NA  2
1   2   2   2

      

I was looking for a similar type function rowMeans

with na.rm = T but couldn't find a suitable function for this situation. I really appreciate any help

+3


source to share


2 answers


We can use apply

c MARGIN = 1

to find the frequency of the numbers in each row and get the maximum frequency number usingwhich.max



test$D <- apply(test, 1, FUN = function(x) {
        x1 <- table(factor(x, levels = unique(x)))
          as.numeric(names(x1)[which.max(x1)])})
test$D
#[1] 1 2 2

      

+1


source


Another option using table

,



apply(test, 1, function(i) as.numeric(names(sort(-table(i)))[1]))
#[1] 1 2 2

      

+2


source







All Articles