Muhammad Azam
2008-May-28 09:23 UTC
[R] request: which integer in each column is in majority
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 best regards Muhammad Azam Ph.D. Student Department of Medical Statistics, Informatics and Health Economics University of Innsbruck, Austria [[alternative HTML version deleted]]
jim holtman
2008-May-28 09:35 UTC
[R] request: which integer in each column is in majority
try this:> 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> apply(x, 2,function(.col)dimnames(table(.col))[[1]][which.max(table(.col))]) [1] "1" "3" "4" "3">On Wed, May 28, 2008 at 5:23 AM, Muhammad Azam <mazam72@yahoo.com> 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 > > > 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@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
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.
Karl Ove Hufthammer
2008-May-28 09:41 UTC
[R] request: which integer in each column is in majority
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 ? ?3As 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