title: get(paste0(...)) overwrites results in R subarray
body: <p>I have two subarrays and would like to sample one of them at random. The subarrays consist of character labels from a larger pool of labels as follows:</p>
  
  <pre><code>K &lt;- as.character(1:10)
  </code></pre>
  
  <p>Suppose I call the arrays K1 and K2 and assign 10 labels between them as follows:</p>
  
  <pre><code>K1 &lt;- 1:8
  K2 &lt;- 9:10
  </code></pre>
  
  <p>I then use</p>
  
  <pre><code>get(paste0("K", i)) 
  </code></pre>
  
  <p>to retrieve the labels that were sampled using a 'for' loop.</p>
  
  <p>The issue I am having is this: if K1 is sampled, get(paste0("K", i)) returns labels from K2 (which contain labels 9:10) instead of returning K1 as needed.</p>
  
  <p>In other words, I believe get(past0("K", i)) may be overwriting results.</p>
  
  <p>Any thoughts on why this may occur and how to go about circumventing the issue?</p>
  
  <p>Below is a more extensive example:</p>
  
  <pre><code>K &lt;- 2
  N &lt;- 100
  Hstar &lt;- 10
  perms &lt;- 10000
  
  probs &lt;- rep(1/Hstar, Hstar)
  
  specs &lt;- 1:N
  
  pop &lt;- array(dim = c(c(perms, N), K))
  
  haps &lt;- as.character(1:Hstar)
  
  K1 &lt;- 1:8 # subarray 1
  K2 &lt;- 9:10 # subarray 2
  
  for(j in 1:perms){
      for(i in 1:K){ 
          if(i == 1){
              pop[j, specs, i] &lt;- sample(haps, size = N, replace = TRUE, prob = probs)
      }
          else{
              pop[j ,, 1] &lt;- sample(haps[K1], size = N, replace = TRUE, prob = probs[K1])
              pop[j ,, 2] &lt;- sample(haps[K2], size = N, replace = TRUE, prob = probs[K1]) 
          }
      }
  }
  
  HAC.mat &lt;- array(dim = c(c(perms, N), K))
  
  for(k in specs){
      for(j in 1:perms){
          for(i in 1:K){ 
              ind.index &lt;- sample(specs, size = k, replace = FALSE)
              hap.plot &lt;- pop[sample(1:nrow(pop), size = 1, replace = TRUE), ind.index, sample(i, size = 1, replace = TRUE)] 
              HAC.mat[j, k, i] &lt;- length(unique(hap.plot))
          }
      }
  }
  
  
  means &lt;- apply(HAC.mat, MARGIN = 2, mean)
  lower &lt;- apply(HAC.mat, MARGIN = 2, function(x) quantile(x, 0.025))
  upper &lt;- apply(HAC.mat, MARGIN = 2, function(x) quantile(x, 0.975))
  
  d &lt;- data.frame(specs, means, lower, upper)
  
  par(mfrow = c(1, 2))
  
      if(i == 1){
          plot(specs, means, type = "n", xlab = "Specimens sampled", ylab = "Unique haplotypes",  ylim = c(1, Hstar))
          polygon(x = c(specs, rev(specs)), y = c(lower, rev(upper)), col = "gray")
          lines(specs, means, lwd = 2)
          HAC.bar &lt;- barplot(N*probs, xlab = "Unique haplotypes", ylab = "Specimens sampled", names.arg = 1:Hstar)
  }
  else if(i &gt; 1){
      plot(specs, means, type = "n", xlab = "Specimens sampled", ylab = "Unique haplotypes", ylim = c(1, Hstar))
      polygon(x = c(specs, rev(specs)), y = c(lower, rev(upper)), col = "gray")
      lines(specs, means, lwd = 2)
      HAC.bar &lt;- barplot(N*probs[get(paste0("K", i))], xlab = "Unique haplotypes", ylab = "Specimens sampled", names.arg = get(paste0("K", i))) ## The issue may lie here
  }
  </code></pre>
  
  <p>Any advice on what may be going on here is greatly appreciated.</p>