Jean Eid <jeaneid <at> chass.utoronto.ca> writes: : : 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
The two calls to apply in this last statement can be simplified by eliminating the indices: Y[which.max(apply(Y, 1, function(y) sum( apply(X, 1, identical, y)))),] Also, although less efficient, its interesting to note that the two apply calls without the sum can be regarded as multiplying Y times X transpose under the identical() inner product: # multiply x times y' under the inner product f. # With f <- function(x,y)sum(x,y) it corresponds to ordinary # matrix multiplication of a times b transpose. xyt <- function(a,b,f) apply(b,1,function(x)apply(a,1,function(y)f(x,y))) # factoring out sum this generalized multiplication gives us: Y[which.max(colSums( xyt(Y,X,identical) )),] ______________________________________________ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
