[R] request: which integer in each column is in majority

Karl Ove Hufthammer Karl.Hufthammer at math.uib.no
Wed May 28 11:41:02 CEST 2008


Muhammad Azam:

> I am one of the new R user. I have a problem regarding to know which of
> the integer in each column of the following matrix is in majority. I want
> to know that integer e.g. in the first column 1 is in majority.
>
>> x=matrix(c(1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,4,1,2,3,3),ncol=4)
>> x
> [,1] [,2] [,3] [,4]
> [1,]    1    2    3    4
> [2,]    1    2    4    1
> [3,]    1    3    4    2
> [4,]    2    3    4    3
> [5,]    2    3    4    3

As long as the matrix only contains integers, the following should work:

apply(x, 2, function(z) which.max(tabulate(z)) )

Output: 1 3 4 3

-- 
Karl Ove Hufthammer



More information about the R-help mailing list