user1723765
user1723765

Reputation: 6389

Select majority number of each row in matrix using r

I have a matrix with varing number of columns of the following form:

1  10  10 10 15
2  14  14 13 13
4  19  19 20 21
6  32  32 20 15

I would like to select the majority for each row producing the following output:

1  10
2  14/13
3  19
4  32

Upvotes: 8

Views: 3928

Answers (3)

user1317221_G
user1317221_G

Reputation: 15441

late addition, but when values are all positive, as in your example, you can also:

apply(x, 1, function(idx) {
     which(tabulate(idx) == max(tabulate(idx)))
     })

without first column:

 apply(x[,-1], 1, function(idx) {
     which(tabulate(idx) == max(tabulate(idx)))
     })

and finally tweak your output:

s <- apply(x[,-1], 1, function(idx) {
     which(tabulate(idx) == max(tabulate(idx)))
     })
sapply(s, paste, sep="", collapse="/")

[1] "10"    "13/14" "19"    "32"   

Upvotes: 4

Matthew Lundberg
Matthew Lundberg

Reputation: 42629

Seemed like table almost gives what you need, but the output must be massaged. Compose is an interesting way to do this:

require(functional)
apply(m, 1, Compose(table,
                    function(i) i==max(i),
                    which,
                    names,
                    function(i) paste0(i, collapse='/')
                    )
      )

## [1] "10"    "13/14" "19"    "32"   

Upvotes: 12

Arun
Arun

Reputation: 118779

One of the possible answers:

# over each row of data.frame (or matrix)
sapply(1:nrow(x), function(idx) {
# get the number of time each entry in df occurs
    t <- table(t(x[idx, ]))
# get the maximum count (or frequency)
    t.max <- max(t)
# get all values that equate to maximum count
    t <- as.numeric(names(t[t == t.max]))
})

Upvotes: 9

Related Questions