Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select majority number of each row in matrix using r

Tags:

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
like image 368
user1723765 Avatar asked Jan 01 '13 20:01

user1723765


1 Answers

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"   
like image 176
Matthew Lundberg Avatar answered Sep 19 '22 04:09

Matthew Lundberg