title: R: How many element of list 1 are in list 2/Number of occurences
body: <p>I would like to compare two lists (two rows of a data frame) and count how many differences I have between the two lists.</p>
  
  <p>for instance:</p>
  
  <pre><code>list1=a,b,c,a
  list2=a,a,d,d
  </code></pre>
  
  <p>two elements of list 1 are in list 2</p>
  
  <p>I am able to do that with a loop and sum but it is very inefficient. Is there any function to do that in R?</p>
  
  <p>I have checked setdiff and the compare package but did not find anything that helps.</p>
  
  <p>Thanks for your ideas,</p>
  
  <p>Vincent</p>
  
  <p>My function looks like:</p>
  
  <pre><code>        NRebalancing=function(NamePresent)
          {
            Nbexchange=NamePresent[,2]
            Nbexchange=NamePresent[1,2]=0
  
            for (i in 2:nrow(NamePresent))
            {
              print(i)
              compteur=0
              NameNeeded=NamePresent[i,]
              NameNeeded=unique(NameNeeded)
              NameNeeded=na.omit(NameNeeded)
              for(j in 2:length(NameNeeded))
                #j=1 correspond a une date
              {
                compteur = compteur+(abs(sum(NamePresent[i,]==as.character(NameNeeded[j]))-sum(NamePresent[i-1,]==as.character(NameNeeded[j]))))
              }
            Nbexchange[i]=compteur  
            }
  
            return(Nbexchange)
          }
  </code></pre>