title: R - Comparing values in a vector to a single value (Using the apply family)
body: <p>I Have this database (shortened)</p>
  
  <pre><code>&gt; head(testDB,25)
   Civilite Age Enfants Region Recence Frequence_visite_site Mailing_Clic
  1      Mlle  18     Non   Nord       5                     5          Oui
  2      Mlle  18     Non    Sud       3                     5          Oui
  3      Mlle  18     Non   Nord       3                     8          Non
  4      Mlle  18     Non   Nord       7                     6          Non
  5      Mlle  18     Non   Nord      10                     5          Non
  6        Mr  18     Non   Nord       4                     8          Oui
  7        Mr  19     Non    Sud       4                     7          Oui
  8      Mlle  20     Non    Sud       4                     8          Oui
  9      Mlle  20     Non    Sud       3                     4          Oui
  10     Mlle  20     Non   Nord       9                     4          Oui
  11     Mlle  20     Non   Nord       5                     7          Oui
  12     Mlle  20     Non   Nord       3                     5          Non
  13     Mlle  20     Non   Nord      12                     4          Non
  14     Mlle  20     Non   Nord      15                     2          Non
  15     Mlle  20     Non   Nord       9                     7          Non
  16     Mlle  20     Non   Nord      18                     5          Non
  17     Mlle  20     Non   Nord      16                     4          Non
  18     Mlle  20     Non   Nord      12                     8          Non
  19     Mlle  20     Non   Nord      11                     8          Non
  20     Mlle  21     Non    Sud       4                     6          Oui
  21     Mlle  21     Non    Sud      15                     8          Oui
  22     Mlle  21     Non    Sud       4                     8          Oui
  23     Mlle  21     Non    Sud       4                     2          Oui
  24     Mlle  21     Non    Sud       6                     4          Oui
  25       Mr  21     Non   Nord       4                     8          Oui
  </code></pre>
  
  <p>and these, my first center of classes:</p>
  
  <pre><code>&gt; K1 = 4
  CenterOfClasses = testDB[sample(1:nrow(testDB), K1, replace=FALSE), ]
  CenterOfClasses
  Civilite Age Enfants Region Recence Frequence_visite_site Mailing_Clic
  Mlle     45     Non   Nord      11                     6          Non
  Mr       37     Non   Nord      10                     7          Non
  Mr       56     Non   Nord      14                     6          Non
  Mlle     43     Non   Nord      11                     8          Non
  </code></pre>
  
  <p>I have a problem when trying to calculate the differences between a vector (being the individual records of testDB) and an element (being each of the elements of my center of classes). </p>
  
  <p>These are my variables:</p>
  
  <pre><code>DVar = list()
  VectClasses ##Vector
  VectIndVar  ##Vector
  
  VectClasses1 = CenterOfClasses[1,] etc
  VectIndVar = testBD
  </code></pre>
  
  <p>This is the function I am trying to use</p>
  
  <pre><code>mapply(function(x, y){
    sum(identical(x,y))},
  VectClasses[x,y],VectIndVar[,y])
  </code></pre>
  
  <p>This is what I want to do :</p>
  
  <pre><code>DVar[[nClass]][nVar]=sum(VectClasses[nClass,nVar] == VectIndVar[,nVar])
  </code></pre>
  
  <p>(compare the 1st column of my database to the value assigned to the first class for the first variable, and that for each variables and classes there might be)</p>
  
  <p>I changed my approach to now compare each record of database to each row of the VectClasses</p>
  
  <p>I have tried this :</p>
  
  <pre><code>xall = data.frame()
  for (k in 1:nrow(VectClasses)) 
   {
     for (i in 1:nrow(VectIndVar))
     {
       xall[i,k] = sum(VectClasses[k,] == VectIndVar[i,])
      }
   }
  </code></pre>
  
  <p>It returns :</p>
  
  <pre><code>  &gt; head(xall)
    V1 V2 V3 V4
  1  3  3  2  2
  2  2  2  1  1
  3  3  4  3  3
  4  3  4  3  3
  5  4  4  3  3
  6  3  2  3  3
  </code></pre>
  
  <p>Now, I tried to cut the loops for and replace it with an apply-family function</p>
  
  <pre><code>xball = sapply(xball, function(i,k){sum(VectClasses[k,] == VectIndVar[i,])})
  </code></pre>
  
  <p>But it doesn't fill xball</p>
  
  <p>I would like to avoid as much as possible to loop over the number of classes, or the number of variables.</p>
  
  <p>Can you help me?</p>