title: Split character and numeric string with NAs
body: <p>For an example dataframe:</p>
  
  <pre><code>df &lt;- structure(list(ID = 1:6, region = structure(c(5L, 3L, 1L, 4L, 
                                                      6L, 2L), .Label = c("AB1", "AB22", "AC225", "AF32", "AX11", "OI222"
                                                      ), class = "factor"), level = c(2L, 3L, 1L, 2L, 3L, 2L)), .Names = c("ID", 
                                                                                                                           "region", "level"), class = "data.frame", row.names = c(NA, -6L
                                                                                                                           ))
  </code></pre>
  
  <p>I have a column called region with at least two character variables infront, and then either 1, 2 or 3 numeric variables.  The level variable denotes how big the region is (where level 1 represents the largest regions in countries and level 3 contains the smallest regions).  </p>
  
  <p>The region codes detail not only what region each ID refers to, but often (where applicable) the larger regions which are linked.</p>
  
  <p>For example, AC225 is:
  AC in region 0; AC2 in region 1; AC22 in region 2; AC225 in region 3.</p>
  
  <p>I wish to do this for each of the ID variables using the region code.  Things are complicated slightly as for some IDs, I will not know the region 2 or 3 (and I then would wish NAs to be added).  For example, for AB32 I would want AB for region.0, AB3 for region.1, AB32 for region.2 and NA for region.3.</p>
  
  <p>I tried to use the stringr function, but haven't had much success:</p>
  
  <pre><code>library(stringr)
  df$region.0 &lt;- str_sub(df$region,1,2)
  df$region.1 &lt;- str_sub(df$region,1,2,3)
  df$region.2 &lt;- str_sub(df$region,1,2,3,4)
  df$region.3 &lt;- str_sub(df$region,1,2,3,4,5)
  </code></pre>
  
  <p>If anyone could help me, I should be most grateful.</p>