title: Is there a more efficient way to replace NULL with NA in a list?
body: <p>I quite often come across data that is structured something like this:</p>
  
  <pre><code>employees &lt;- list(
      list(id = 1,
               dept = "IT",
               age = 29,
               sportsteam = "softball"),
      list(id = 2,
               dept = "IT",
               age = 30,
               sportsteam = NULL),
      list(id = 3,
               dept = "IT",
               age = 29,
               sportsteam = "hockey"),
      list(id = 4,
               dept = NULL,
               age = 29,
               sportsteam = "softball"))
  </code></pre>
  
  <p>In many cases such lists could be tens of millions of items long, so memory concerns and efficiency are always a concern. </p>
  
  <p>I would like to turn the list into a dataframe but if I run:</p>
  
  <pre><code>library(data.table)
  employee.df &lt;- rbindlist(employees)
  </code></pre>
  
  <p>I get errors because of the NULL values. My normal strategy is to use a function like:</p>
  
  <pre><code>nullToNA &lt;- function(x) {
      x[sapply(x, is.null)] &lt;- NA
      return(x)
  }
  </code></pre>
  
  <p>and then:</p>
  
  <pre><code>employees &lt;- lapply(employees, nullToNA)
  employee.df &lt;- rbindlist(employees)
  </code></pre>
  
  <p>which returns</p>
  
  <pre><code>   id dept age sportsteam
  1:  1   IT  29   softball
  2:  2   IT  30         NA
  3:  3   IT  29     hockey
  4:  4   NA  29   softball
  </code></pre>
  
  <p>However, the nullToNA function is very slow when applied to 10 million cases so it'd be good if there was a more efficient approach. </p>
  
  <p>One point that seems to slow the process down it the is.null function can only be applied to one item at a time (unlike is.na which can scan a full list in one go). </p>
  
  <p>Any advice on how to do this operation efficiently on a large dataset?</p>