title: A way to extract the name of subarray that was sampled in R
body: <p>I have a simulation that I have constructed and need to refer to a given subarray that was randomly sampled from in order to create some plots using a <code>for</code> loop. I am not quite certain how to go about accomplishing this...</p>
  
  <p>Here is my code:</p>
  
  <pre><code>K &lt;- 2       # number of subarrays
  N &lt;- 100 
  Hstar &lt;- 10
  
  perms &lt;- 10000
  
  p &lt;- 0.95
  
  specs &lt;- 1:N 
  
  pop &lt;- array(dim = c(c(perms, N), K))
  
  haps &lt;- as.character(1:Hstar)
  
  probs &lt;- rep(1/Hstar, Hstar) 
  
  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{
              K1 &lt;- c(1:5)
              K2 &lt;- c(6:10)
              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[K2]) 
          }
      }
  }
  
  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(1:K, 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))
  
  par(mfrow = c(1, 2))
  
  for(i in 1:K){
      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{
          plot(specs, means, type = "n", xlab = "Specimens sampled", ylab = "Unique haplotypes", ylim = c(1, max(means)))
          polygon(x = c(specs, rev(specs)), y = c(lower, rev(upper)), col = "gray")
          lines(specs, means, lwd = 2)
          HAC.bar &lt;- barplot(max(HAC.mat)*probs[K1], xlab = "Unique haplotypes", ylab = "Specimens sampled", names.arg = min(means):ceiling(max(means)))
      }
  }
  </code></pre>
  
  <p>The issue is in the last else statement where I indicate</p>
  
  <pre><code>max(HAC.mat)*probs[K1]
  </code></pre>
  
  <p>Above, I just put K1 as an illustration. However, in reality, I don't know which row of K1 or K2 was sampled. Is there a way to specify this generically? Something like K[i]?</p>
  
  <p>Thanks! Please let me know if more clarification is needed. </p>