title: Merge two data frames in R according to ID-variable
body: <p>I am trying to merge two data frames in R according to ID-variable , since in the first data frame the ID-variable is a factor and in the second data frame the ID-variable is a numeric vector</p>
  
  <pre><code>datC &lt;- data.frame("ID" = c("001","010","100","110"), "X" = rnorm(4))
  datD &lt;- data.frame("ID" = c(001,010,100,110,210), "Y" = rpois(5,3))
  merge(datC,datD,by="ID")
  
  ID           X  Y
  100  0.03284493 2
  110 -0.41690986 3
  </code></pre>
  
  <p>As we see the 001,010 are missing , and it is clear for me why they are missing 
  and how I can solve this </p>
  
  <pre><code>datC$ID &lt;- as.numeric(as.character(datC$ID))
  merge(datC,datD,by="ID")
  </code></pre>
  
  <p>I have tried to find another solution by let R avoid TO treat a vector of character as a factor</p>
  
  <pre><code>datC &lt;- data.frame("ID" = c("001","010","100","110"), "X" = rnorm(4),stringsAsFactors = F)
  datD &lt;- data.frame("ID" = c(001,010,100,110,210), "Y" = rpois(5,3))
  </code></pre>
  
  <p>When I merged these two data frames according to the ID-variable , I expected that I will get null results , since we have a character vs numeric vectors but in fact R gave me the same results as the ID-Variable in the first data frame is a factor .</p>
  
  <pre><code>merge(datC,datD,by="ID")
  
  ID          X  Y
  100 -0.2797956 4
  110 -1.0397791 4
  </code></pre>
  
  <p>So can someone explain me why my expectation was false !</p>