TDo
TDo

Reputation: 744

R - choose the number which appears most in a row

I have a df test:

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

I want to create a another column, say test$D, which is the number that appears most in that row, excluding NA. My desired df is:

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

I have been looking for a similar function like rowMeans with na.rm=T but could not find any appropriate function for this situation. Really appreciate any help

Upvotes: 3

Views: 63

Answers (2)

Sotos
Sotos

Reputation: 51592

Another option using table,

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

Upvotes: 2

akrun
akrun

Reputation: 887501

We can use apply with MARGIN = 1 to find the frequency of numbers in each row and get the maximum frequency number using which.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

Upvotes: 1

Related Questions