title: How to get number of mismatch between two categorical vectors
body: <p>Good afternoon ,</p>
  <p>Assume we have the following dataset :</p>
  <pre><code>data_cat=structure(list(c(&quot;X266&quot;, &quot;B7&quot;, &quot;T133&quot;, &quot;J34&quot;, &quot;T218&quot;, &quot;X249&quot;), 
      c(&quot;TVGUIDE&quot;, &quot;MODMAT&quot;, &quot;MASSEY&quot;, &quot;KMART&quot;, &quot;MASSEY&quot;, &quot;ROSES&quot;
      ), c(&quot;YES&quot;, &quot;YES&quot;, &quot;YES&quot;, &quot;NO&quot;, &quot;YES&quot;, &quot;NO&quot;), c(&quot;KEY&quot;, &quot;KEY&quot;, 
      &quot;KEY&quot;, &quot;KEY&quot;, &quot;KEY&quot;, &quot;KEY&quot;), c(&quot;YES&quot;, &quot;YES&quot;, &quot;YES&quot;, &quot;YES&quot;, 
      &quot;YES&quot;, &quot;YES&quot;), c(&quot;BENTON&quot;, &quot;BENTON&quot;, &quot;BENTON&quot;, &quot;BENTON&quot;, 
      &quot;BENTON&quot;, &quot;BENTON&quot;), c(&quot;GALLATIN&quot;, &quot;GALLATIN&quot;, &quot;GALLATIN&quot;, 
      &quot;GALLATIN&quot;, &quot;GALLATIN&quot;, &quot;GALLATIN&quot;), c(&quot;UNCOATED&quot;, &quot;UNCOATED&quot;, 
      &quot;UNCOATED&quot;, &quot;UNCOATED&quot;, &quot;UNCOATED&quot;, &quot;COATED&quot;), c(&quot;UNCOATED&quot;, 
      &quot;COATED&quot;, &quot;UNCOATED&quot;, &quot;COATED&quot;, &quot;UNCOATED&quot;, &quot;COATED&quot;), c(&quot;NO&quot;, 
      &quot;NO&quot;, &quot;NO&quot;, &quot;NO&quot;, &quot;NO&quot;, &quot;NO&quot;), c(&quot;LINE&quot;, &quot;LINE&quot;, &quot;LINE&quot;, 
      &quot;LINE&quot;, &quot;LINE&quot;, &quot;LINE&quot;), c(&quot;YES&quot;, &quot;YES&quot;, &quot;YES&quot;, &quot;YES&quot;, &quot;YES&quot;, 
      &quot;YES&quot;), c(&quot;Motter94&quot;, &quot;WoodHoe70&quot;, &quot;WoodHoe70&quot;, &quot;WoodHoe70&quot;, 
      &quot;WoodHoe70&quot;, &quot;Motter94&quot;), c(&quot;TABLOID&quot;, &quot;CATALOG&quot;, &quot;CATALOG&quot;, 
      &quot;TABLOID&quot;, &quot;CATALOG&quot;, &quot;TABLOID&quot;), c(&quot;NorthUS&quot;, &quot;NorthUS&quot;, 
      &quot;NorthUS&quot;, NA, &quot;NorthUS&quot;, &quot;CANADIAN&quot;), c(&quot;noband&quot;, &quot;noband&quot;, 
      &quot;noband&quot;, &quot;noband&quot;, &quot;noband&quot;, &quot;noband&quot;)), row.names = c(NA, 
  6L), class = &quot;data.frame&quot;)
  </code></pre>
  <p>I need to calculate number of mismatch (elements that differs ) between two rows / vectors. For this i developped the following function :</p>
  <pre><code>char_dist_pairs&lt;-function(x,y){
    
    length_charachter_part=length(which(sapply(x,class)==&quot;factor&quot;|sapply(x,class)==&quot;character&quot;|sapply(x,class)==&quot;logical&quot;))
    
    
    
    m=length_charachter_part-as.numeric(table(x==y)[&quot;TRUE&quot;])
    
    m=replace(m,is.na(m),0)
    
    return(m)
  }
  </code></pre>
  <p>This function works with another dataset as expected :</p>
  <pre><code>ballons=structure(list(YELLOW = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
  2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c(&quot;PURPLE&quot;, 
  &quot;YELLOW&quot;), class = &quot;factor&quot;), SMALL = structure(c(2L, 2L, 2L, 
  2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L
  ), .Label = c(&quot;LARGE&quot;, &quot;SMALL&quot;), class = &quot;factor&quot;), STRETCH = structure(c(2L, 
  2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 
  1L, 1L), .Label = c(&quot;DIP&quot;, &quot;STRETCH&quot;), class = &quot;factor&quot;), ADULT = structure(c(1L, 
  2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 
  1L, 2L), .Label = c(&quot;ADULT&quot;, &quot;CHILD&quot;), class = &quot;factor&quot;), T = c(TRUE, 
  FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, 
  FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE)), class = &quot;data.frame&quot;, row.names = c(NA, 
  -19L))
  
  char_dist_pairs(ballons[1,],ballons[2,])
  #output :
  2
  </code></pre>
  <p>This is right because the <code>fourth</code> and <code>fifth</code> elements are different for the two vectors :</p>
  <pre><code>&gt; ballons[1,]
  1 YELLOW SMALL STRETCH ADULT TRUE
  &gt; ballons[2,]
  2 YELLOW SMALL STRETCH CHILD FALSE
  </code></pre>
  <p>However i'm unable to get the expected output for the first dataset :</p>
  <pre><code>char_dist_pairs(data_cat[1,],data_cat[2,])
  [1] 0
  </code></pre>
  <p>Here normally i should get 5 instead of 0 . I also noticed that :</p>
  <pre><code>&gt; data_cat[1,]==data_cat[2,]
  1
  
  &gt; ballons[1,]==ballons[2,]
    
  1   TRUE  TRUE    TRUE FALSE FALSE
  </code></pre>
  <p>This means i can't do a logical comparison for the first dataset.</p>
  <p>Thank you for help !</p>