[R] finding the most frequent row

Jean Eid jeaneid at chass.utoronto.ca
Fri Dec 10 15:59:57 CET 2004


You can do the following also




X <- matrix(c(1,2,1,2,1,3,1,4), ncol=2)
Y<-unique(X)
Y[which.max(apply(Y, 1,function(i) sum(apply(matrix(1:nrow(X)),
1,function(x) identical(X[x,], i[1:2]))))),]


I do not know what your strategy is when there are multiple maxima i.e two
different  rows appear at the same frequency. The above will ge the first
row that appears to be the maximum  as an example suppose that we have

X <- matrix(c(1,2,1,2,1,3,1,4, 1, 4), ncol=2)
The above will have [2,1] as the maximum row while it should be both [2,1]
and [1,4]. To correct that try
Y[(yy <- (apply(Y, 1,function(i) sum(apply(matrix(1:nrow(X)),
1,function(x) identical(X[x,], i[1:2]))))))%in%max(yy),]


and this will give you [2,1] and [1,4] as the most frequent rows.


Hopw this help


Jean










On Fri, 10 Dec 2004, bogdan romocea wrote:

> Here's something that works. I'm sure there are better solutions (in
> particular the paste part - I couldn't figure out how to avoid typing
> a[i,1], ..., a[i,10]).
>
> a <- matrix(nrow=1000,ncol=10)
> for (i in 1:1000)
> 	for (j in 1:10)
> 		a[i,j] <- sample(1:0,1)
>
> b <- vector(mode="character")
> for (i in 1:1000)
> 	b[i] <- paste(a[i,1],a[i,2],a[i,3],a[i,4],a[i,5],
> 		a[i,6],a[i,7],a[i,8],a[i,9],a[i,10],sep="")
>
> #the most frequent row
> table(b)[table(b) == max(table(b))]
>
> HTH,
> b.
>
>
> -----Original Message-----
> From: Lisa Pappas [mailto:lisa.pappas at hci.utah.edu]
> Sent: Thursday, December 09, 2004 5:15 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] finding the most frequent row
>
>
> I am bootstrapping using a function that I have defined.  The
> "Statistic" of the function is an array of 10 numbers.  Therefore if
> I use 1000 replications,  the "t" matrix will have 1000 rows each of
> which is a bootstrap replicate of this 10 number array (10 columns).
> Is there any easy way in R to determine which row appears the most
> frequently?
>
> Thanks,
> Lisa Pappas
>
> Huntsman Cancer Institute wishes to promote open communication while
> protecting confidential and/or privileged information.  If you have
> received this message in error, please inform the sender and delete
> all copies.
>
> ______________________________________________
> 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
>
> ______________________________________________
> 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
>




More information about the R-help mailing list