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

From: Uwe Ligges <ligges_at_statistik.tu-dortmund.de>
Date: Wed, 28 May 2008 11:39:22 +0200

Muhammad Azam wrote:
> Respected R helpers/ users
> 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. Similarly in the third column 4 is in majority. So what is the suitable way to get the desired integer for each column. I am looking for some kind reply. Thanks
> example:

>> 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
>

Probably bad but working example:

apply(x, 2, function(x) as.numeric(names(which.max(table(x)))[1]))

Uwe Ligges

> best regards
>
> Muhammad Azam
> Ph.D. Student
> Department of Medical Statistics,
> Informatics and Health Economics
> University of Innsbruck, Austria
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 28 May 2008 - 11:50:12 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 28 May 2008 - 12:30:42 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive