[R] The most common row in a matrix?

Gabor Grothendieck ggrothendieck at gmail.com
Sun Nov 19 21:52:47 CET 2006


Try this:

a <- matrix(1:3, 4, 5)

a.ag <- aggregate(1:nrow(a), as.data.frame(a), length)
a.ag[which.max(a.ag$x), 1:ncol(a)]


On 11/19/06, kone <attenka at utu.fi> wrote:
> Hi,
>
> How do you get the most common row from a matrix? If I have a matrix
> like this
>
> array(1:3,dim=c(4,5))
>
>      [,1] [,2] [,3] [,4] [,5]
> [1,]    1    2    3    1    2
> [2,]    2    3    1    2    3
> [3,]    3    1    2    3    1
> [4,]    1    2    3    1    2
>
> in which rows 1 and 4 are similar, I want to find that vector c
> (1,2,3,1,2).
>
> Atte Tenkanen
> University of Turku, Finland
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>



More information about the R-help mailing list