Muhammad Azam wrote:
Thanks for the effort but still we are far from the desired result. May be this 
example will help you to understand the situation. Example
a1=c(1:12);   a1=array(a1,dim=c(3,4));   a2=c(1:12);  a2=array(a2,dim=c(3,4));  
a3=c(1:16)
a3=array(a3,dim=c(4,4)); a=list(a1,a2,a3); a
[[1]]
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12

[[2]]
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12

[[3]]
     [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2    6   10   14
[3,]    3    7   11   15
[4,]    4    8   12   16

Here [[1]] and [[2]] are same out of three (internal values wise). The whole 
array [[1]] or [[2]] is in majority. So i want to get the whole array or 
component of list which is in majority. The result should be like this
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
Hi Muhammad,
If I have understood the problem, you want to select one or more components of a list containing matrices, arrays or data frames that are least dissimilar to one or more other components of that list. Dissimilarity is defined by the sum of mismatching elements. Because you can have more than one set of pairs that are "least dissimilar", the attached function returns the indices of the "least dissimilar" pairs and the "least dissimilar" components. Hope it does what you want.

Jim


find.most.repeated<-function(x) {
 # this will only work if all components are matrices, arrays or data frames
 ncomponents<-length(x)
 if(ncomponents == 1) stop("Can't compare anything, only one component")
 paircomps<-combn(1:ncomponents,2)
 ncomps<-dim(paircomps)[2]
 compsums<-rep(NA,ncomps)
 for(comp in 1:ncomps) {
  xdim1<-dim(x[[paircomps[1,comp]]])
  xdim2<-dim(x[[paircomps[2,comp]]])
  if(sum(xdim1 != xdim2) == 0)
   compsums[comp]<-sum(x[[paircomps[1,comp]]]  != x[[paircomps[2,comp]]])
  else {
   # get the conformable portion of the components
   mindim<-c(min(c(xdim1[1],xdim2[1])),min(c(xdim1[2],xdim2[2])))
   # count all nonconformable elements as mismatches and add the conformable 
mismatches
   compsums[comp]<-(xdim1[1]-mindim[1])*mindim[2] + 
(xdim1[2]-mindim[2])*mindim[1] +
    sum(x[[paircomps[1,comp]]][1:mindim[1],1:mindim[2]] !=
    x[[paircomps[2,comp]]][1:mindim[1],1:mindim[2]])
  }
 }
 # collect the indices of the comparisons with the least mismatches
 bestmatches<-which(compsums == min(compsums))
 return(list(bestmatches=paircomps[,bestmatches],components=x[bestmatches]))
}
______________________________________________
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
and provide commented, minimal, self-contained, reproducible code.

Reply via email to