title: Recoding empty spaces in a dataset to NA using R
body: <p>I have a dataset with lots of empty spaces ("" in place of NA).I would like to recode those empty spaces as NA (i would like to use some imputation packages like Mice from R ecosystem). To this end, I am trying the following code:</p>
  
  <pre><code>emptyToNA &lt;- function(x){
    y &lt;- data.frame(matrix(NA,nrow=nrow(x),ncol=ncol(x)))
    for(i in ncol(x)){
      for(j in nrow(x)){
        if(x[j,i] != ""){
          y[j,i] = x[j,i]
        }
      }
    }
    return(y)
  }
  </code></pre>
  
  <p>However, I am getting this error:<code>Error in if (x[j, i] != "") { : missing value where TRUE/FALSE needed</code>. So I believe the condition within the <code>if</code> clause is not correctly evaluating.</p>
  
  <p>But when I am just checking <code>data[j,i] != ""</code> for some values of <code>i</code> and <code>j</code>, it correctly evaluates to <code>TRUE/FALSE</code>. The error occurs only when I am using the function.</p>
  
  <p>I couldn't figure out why. Any help would be greatly appreciated.</p>
  
  <p>Thanks</p>