title: Need to change data.table columns' value from "Yes" ,"No" to 1,0
body: <p>I have a datatable object of around 0.8 million rows. Some columns in them have <code>Yes</code>/<code>No</code> values. I want to change it to <code>1</code>/<code>0</code> respectively. Here <code>workable_data</code> is my data.table object:</p>
  
  <p>My code for changing (which seems too simplistic):</p>
  
  <pre><code>yes_No_to_1 &lt;- function(x){
    sapply(x,function(x){
      if(x =='Yes'){
        1
        }else{
          0}
      })
  } 
  
  sapply(workable_data[,product,with=FALSE],yes_No_to_1)
  </code></pre>
  
  <p>However there are two problem s: (1) it's taking a long time and (2) I want to change the values permanently in <code>workable_data</code> in a <em>data.table</em> way.</p>
  
  <p>You can reproduce sample data with:</p>
  
  <pre><code>product &lt;- c("HasProduct1","HasProduct2","HasProduct3","HasProduct4","HasProduct5","HasProduct6","HasProduct7","HasProduct8","HasProduct9","HasProduct10")
  workable_data &lt;- as.data.table(data.frame(x=sample(1:100),sapply(product,function(x){x &lt;-sample(c("Yes","No"),10000,replace = T)})))
  </code></pre>
  
  <p>I assume there is a better way to achieve this but I cant seem to figure it out and I would also appreciate any advise on optimization of my function (that is going to be the last resort). The time taken in this sample data is, which is pretty bad for 10k rows.</p>
  
  <pre><code>  user    system elapsed 
     6.21    0.00    6.25 
  </code></pre>