title: R if error for subscript out of bounds
body: <p>I'm looping through a nested loop but not sure how to deal with when one index doesn't have any data.</p>
  
  <p>For example:</p>
  
  <pre><code>A &lt;- list(list(1,2), list(2), list(4,5))
  </code></pre>
  
  <p>I can do:</p>
  
  <pre><code>for(i in 1:length(A)){print(A[[i]][[1]])}
  [1] 1
  [1] 2
  [1] 4
  </code></pre>
  
  <p>However the following fails: </p>
  
  <pre><code>for(i in 1:length(A)){print(A[[i]][[2]])}
  [1] 2
  Error in A[[i]][[2]] : subscript out of bounds
  </code></pre>
  
  <p>I cannot simply wrap it in a <code>if(is.null(...)){"Null"}else{...}</code> as I had done previously. </p>
  
  <p>Is there any generic if error or is error function that would give a value whenever I get the subscript out of bounds error? This might not be needed for the above example, but my data is a little bit more complicated and I dont know how to replicate it but will show what I was trying to loop through, what it looks like when empty and when there is a result:</p>
  
  <pre><code>for(i in 1:x$count){z=rbind.fill(z,data.frame(data=if(is.null(x$results[[i]]$experiences[[1]]$experience$A)){"Null"}else{x$results[[i]]$experiences[[1]]$experience$A}
  </code></pre>
  
  <p>Empty:</p>
  
  <pre><code>x$results[[4]]$experiences
  list()
  </code></pre>
  
  <p>result:</p>
  
  <pre><code>x$results[[3]]$experiences
  [[1]]
  [[1]]$experience
  [[1]]$experience$start
  [1] "..."
  
  [[1]]$experience$A
  [1] "..."
  
  [[1]]$experience$B
  [[1]]$experience$B$A
  [1] "..."
  
  
  [[1]]$experience$C
  [[1]]$experience$C$A
  NULL
  
  [[1]]$experience$C$B
  NULL
  
  [[1]]$experience$C$C
  NULL
  
  
  
  [[1]]$`_meta`
  [[1]]$`_meta`$weight
  [1] 1
  
  [[1]]$`_meta`$`_sources`
  [[1]]$`_meta`$`_sources`[[1]]
  [[1]]$`_meta`$`_sources`[[1]]$`_origin`
  [1] "..."
  
  
  
  
  
  [[2]]
  [[2]]$experience
  [[2]]$experience$start
  [1] "..."
  
  [[2]]$experience$A
  [1] "..."
  
  [[2]]$experience$B
  [[2]]$experience$B$C
  [1] "..."
  
  
  [[2]]$experience$C
  [[2]]$experience$C$A
  NULL
  
  [[2]]$experience$C$B
  NULL
  
  [[2]]$experience$C$C
  NULL
  
  
  
  [[2]]$`_meta`
  [[2]]$`_meta`$weight
  [1] 1
  
  [[2]]$`_meta`$`_sources`
  [[2]]$`_meta`$`_sources`[[1]]
  [[2]]$`_meta`$`_sources`[[1]]$`_origin`
  [1] "..."
  
  
  
  
  
  [[3]]
  [[3]]$experience
  [[3]]$experience$start
  [1] "..."
  
  [[3]]$experience$A
  [1] "..."
  
  [[3]]$experience$B
  [[3]]$experience$B$A
  [1] "..."
  
  
  [[3]]$experience$C
  [[3]]$experience$C$A
  NULL
  
  [[3]]$experience$C$B
  NULL
  
  [[3]]$experience$C$C
  NULL
  
  
  
  [[3]]$`_meta`
  [[3]]$`_meta`$weight
  [1] 1
  
  [[3]]$`_meta`$`_sources`
  [[3]]$`_meta`$`_sources`[[1]]
  [[3]]$`_meta`$`_sources`[[1]]$`_origin`
  [1] "..."
  </code></pre>