title: R: ignoring an error in a for loop when assigning a value to a list
body: <p>I am looking to run a for loop on some data looking for certain id codes and then modifying an existing list. I am running into an issue where my list seems to be over written with NULL values; I am sure this is a very simple fix that i can't wrap my brain around. Below is the code:</p>
  <pre><code># my sample data
      sample_data &lt;- 
    data.frame(
      type = c(&quot;dimension&quot;, &quot;dimension&quot;, &quot;dimension&quot;, &quot;dimension&quot;, &quot;dimension&quot;),
      d_id = c(&quot;1234&quot;, &quot;last_year&quot;, &quot;4567&quot;, &quot;897&quot;, &quot;9001&quot;),
      sub_id = c(&quot;dx&quot;, &quot;pe&quot;, &quot;ou&quot;, &quot;ytu&quot;, &quot;iouy&quot;)
    )
  
  # my initial list
  my_list &lt;- list(dx = NULL, ou = NULL, pe = NULL, co = NULL, ao = NULL)
  
  # loop, when there is a value assign it within the list, when an error kicks because there is no value assign null
  # or could simply leave null
  for (dim in c(&quot;dx&quot;, &quot;ou&quot;, &quot;pe&quot;, &quot;co&quot;, &quot;ao&quot;)) {
    
    tryCatch(
      {
        my_list[dim] &lt;- sample_data[sample_data$sub_id == dim &amp; sample_data$type == &quot;dimension&quot;,]$d_id
      },
      error = function(e) {
        my_list[dim] &lt;- NULL
      }
    )
    
    
  }
  </code></pre>
  <p>What I get:</p>
  <pre><code>$dx
  NULL
  
  $ou
  NULL
  
  $pe
  NULL
  
  $co
  NULL
  
  $ao
  NULL
  </code></pre>
  <p>What I expect:</p>
  <pre><code>$dx
  [1] &quot;1234&quot;
  
  $ou
  [1] &quot;4567&quot;
  
  $pe
  [1] &quot;last_year&quot;
  
  $co
  NULL
  
  $ao
  NULL
  </code></pre>