title: r- if inside lapply
body: <p>I have a table like:</p>
  
  <pre><code>                                       dir   E_numdir      last2
  1                                          a          1          1
  2 PJE INDEPENDENCIA 96 5                          96, 5     96,  5
  3 PJE INDEPENDENCIA 96 5                          96, 5     96,  5
  4 B 34 VIVIENDA RECRE 53 00                  34, 53, 00 34, 53, 00
  5 SARMIENTO CASA DE GO 613                          613         13
  6 ROCA 958 00 o                                 958, 00     58, 00
  </code></pre>
  
  <p>I need to evaluate each element of each row and if they are 00 store a list of 1 if they are 00 or 0 if they are not in each row.</p>
  
  <p>I tried:</p>
  
  <pre><code>d$last1&lt;-lapply(d$E_numdir, 
                  function(w) lapply(strsplit(w,","),
                                     function(z) { 
                                       if(str_sub(z, -2, -1)==00){1}
                                     else {0}}))
  </code></pre>
  
  <p>But I get an error:</p>
  
  <blockquote>
    <p>Error in if (str_sub(z, -2, -1) == 0) { : argument is of length zero</p>
  </blockquote>
  
  <p>The desired result would be</p>
  
  <pre><code>                                       dir   E_numdir      last2      xx
  1                                          a          1          1    0
  2 PJE INDEPENDENCIA 96 5                          96, 5     96,  5    0,0 
  3 PJE INDEPENDENCIA 96 5                          96, 5     96,  5    0,0
  4 B 34 VIVIENDA RECRE 53 00                  34, 53, 00 34, 53, 00    0,0,1
  5 SARMIENTO CASA DE GO 613                          613         13    0
  6 ROCA 958 00 o                                 958, 00     58, 00    1,0
  </code></pre>
  
  <p>How can evaluate each element of each row with if function and store it in the same row?</p>
  
  <p>EDITION:
  Structure of the table:</p>
  
  <blockquote>
    <p>dput(d[1:2,c('dir',   'E_numdir',      'last2')])
    structure(list(dir = c("a", "PJE INDEPENDENCIA 96 5                    "
    ), E_numdir = c("1", "96, 5"), last2 = list(list("1"), list(c("96", 
    " 5")))), .Names = c("dir", "E_numdir", "last2"), row.names = 1:2, class = "data.frame")</p>
  </blockquote>