title: Adding a new value into a nested list in R
body: <p>I am using Highcharts for visualization and Highcharter generates charts as a nested list similar to the <code>example_list</code> below. I'm summarizing it here because the original list is much longer and complex.</p>
  
  <pre><code>example_list &lt;- list(
      x = list(
          hc_opts = list(
              series = list(
                  list(group = "group_a", data = list(0,2,4,6)),
                  list(group = "group_b", data = list(0,3,6,9)),
                  list(group = "group_c", data = list(9,4,8,12))))))
  </code></pre>
  
  <p>Just like the group and data nodes, I would like to add an element called <code>type</code> at the level of <code>group</code> and <code>data</code>, but only to the 1st and 3rd elements under <code>series</code>. I'm basically looking for an output same as the following:</p>
  
  <pre><code>example_list &lt;- list(
      x = list(
          hc_opts = list(
              series = list(
                  list(group = "group_a", data = list(0,2,4,6), type = "type_X"),
                  list(group = "group_b", data = list(0,3,6,9)),
                  list(group = "group_c", data = list(9,4,8,12), type = "type_Y")))))
  </code></pre>
  
  <p>I can do this with a for-loop (because I have vectors for locations and types), but
  there should be an elegant way of coding it. My best attempt so far.</p>
  
  <pre><code>locations &lt;- c(1,3)
  types &lt;- c("type_X","type_Y")
  for(i in 1:length(locations)) {
      example_list[["x"]][["hc_opts"]][["series"]][[locations[i]]][["type"]] &lt;- types[i]
  }
  </code></pre>