title: How to correctly convert NULL variables to 0 in an R table
body: <p>I have a table that for some variables has missing data (recorded as NULL) - I'd like to convert some of these missing cells to hold a 0 but for some reason I can't seem to get the syntax correct. My initial approach was to do this:</p>
  
  <pre><code>b&lt;- eval(parse(text=paste(table_full$','column_name1',sep='')))
  b[which(is.na(b))]&lt;-0
  b[which(b=='NULL')]&lt;-0
  </code></pre>
  
  <p>and then save the data to a file, however - this still results in missing data in the output files and warning messages like:</p>
  
  <pre><code>In `[&lt;-.factor`(`*tmp*`, which(is.na(b)), value = 0) :
    invalid factor level, NA generated
  </code></pre>
  
  <p>Alternatively, I've tried things of the form:</p>
  
  <pre><code>b[which(is.na(as.numeric(as.character(b))))]&lt;-0
  </code></pre>
  
  <p>but this didn't resolve the situation.</p>
  
  <p>I'm relatively new to R and can't understand exactly what I'm doing wrong here. Thanks in advance!</p>