title: Missing value introduced in a logical
body: <p>I was working with the inbuilt data set (<code>ggplot2</code>), named <code>diamonds</code>. After having assigned to <code>dia1</code>, I performed the following script, so as to group the values according to their carats. I got a message regarding <code>NAs</code> being introduced by coercion. I fail to understand how that would have happened, as is also apparent by the sum of the <code>is.na()</code> vector equating to zero.</p>
  
  <pre><code>#data
  library(ggplot2)
  dia1 &lt;- diamonds
  
  #logic
  x&lt;-1
  dia1$carat&lt;-as.character(dia1$carat)
  for (i in 1:(length(dia1$carat))){
  
  if (0&lt;(as.numeric(dia1$carat[x]))&amp;(as.numeric(dia1$carat[x]))&lt;=1){
    dia1$carat[x]&lt;-"0-1"
  }
  if (1 &lt; (as.numeric(dia1$carat[x]))&amp;(as.numeric(dia1$carat[x])) &lt;= 2){
    dia1$carat[x]&lt;-"1-2"
  }
  if (2&lt;(as.numeric(dia1$carat[x]))&amp;(as.numeric(dia1$carat[x]))&lt;=3){
    dia1$carat[x]&lt;-"2-3"
  }
  if (3&lt;(as.numeric(dia1$carat[x]))&amp;(as.numeric(dia1$carat[x]))&lt;=4){
    dia1$carat[x]&lt;-"3-4"
  }
  if (4&lt;(as.numeric(dia1$carat[x]))&amp;(as.numeric(dia1$carat[x]))&lt;=5){
    dia1$carat[x]&lt;-"4-5"
  }
    x&lt;-x+1
  }
  </code></pre>
  
  <blockquote>
    <p>Error in if (0 &lt; (as.numeric(dia1$carat[x])) &amp;
    (as.numeric(dia1$carat[x])) &lt;  :    missing value where TRUE/FALSE
    needed In addition: Warning messages: 1: NAs introduced by coercion 
    2: NAs introduced by coercion</p>
  </blockquote>
  
  <pre><code># check if there are any NAs in the data
  sum(is.na(dia1$carat))
  [1] 0
  </code></pre>
  
  <p>Alternatively, why were there no <code>NAs</code> introduced when the <code>dia1$carat</code> vector was explicitly coerced to a character, but there were <code>NAs</code> introduced in the back transformation?</p>