title: Split a column of strings into variable number of columns using data.table in R
body: <p>I want to analyze many years of of Quicken home finance records. I exported a file to qif and used bank2csv program to render a csv. Within Quicken one can use a category (eg automobile, tax), subcategories (eg automobile:service, automobile:fuel) and tags (eg self, spouse, son). bank2csv renders the categories:subcategories/tag as a concatenated string. I want to instead put the category in a category column, subcategory in a subcategory column and put whatever tags in the tag column. I saw <a href="https://stackoverflow.com/questions/18154556/r-split-text-string-in-a-data-table-columns">a similar question</a> but alas that worked by<code>strsplit</code> then <code>unlist</code> and then indexing each element so that it could be written to the correct place by assignment. That will not work here since sometimes there is no tag and sometimes there is no subcategory. It is quite easy to split the string into a list and save that list in a column but how on earth does one assign the first element of the list to one column and the second element (if it exists) to a second column. Surely there is an elegant easy way.</p>
  
  <p>simplified sample </p>
  
  <pre><code>library(data.table)
  library(stringi)
  dt &lt;- data.table(category.tag=c("toys/David", "toys/David", "toys/James", "toys", "toys", "toys/James"), transaction=1:6)
  </code></pre>
  
  <p>How do I create a third and fourth column: category, tag. Some of tag would be <code>NA</code></p>
  
  <p>I can do the following but it does not get me very far. I need a way to specify the first or the second element of the resultant list (as opposed to the whole list)</p>
  
  <pre><code>dt[, category:= strsplit(x = category.tag, split = "/") ]
  </code></pre>