text: user4834: I want to compute the nearest neighbour distances using a for loop. I can do this with a random subset by doing this:
  df <-  read.table("tmp.txt", header=F)
  colnames(df) <- c("gene", "seqnames", "start", "stop", "LOCID")
  gr <- makeGRangesFromDataFrame(df, keep.extra.columns=TRUE)
  
  # genes of interest in a list
  gene.list <- read.table("careplastic_diffs.txt")
  
  # filters the GenomicRanges object with genes of interest
  sub.gr <- gr[(elementMetadata(gr)[, "gene"] %in% gene.list$V1)]
  
  # compute distances
  dists <- distanceToNearest(x = sub.gr)
  
  mcols(dists)[,1] # contains a list of distances
  
  I now want to essentially do this 5 times, using the same logic as above:
  ## create a rand dataframe, each column is a random list of 50 genes
  rand.df <- data.frame(replicate(5, sample(gene.list$V1, 50)))
  
  # loop through columns and gets distances and prints to a file
  for(i in 1:ncol(rand.df)){
    rand.gr[i] <- gr[(elementMetadata(gr)[,"gene"] %in% rand.df[,i])]
    dists[i] <- distanceToNearest(x = rand.gr[i])
    output <- print(mcols(dists[i][,1]))
  }
  
  But I can't seem to make this work.
  
  Error: subscript contains out-of-bounds indices In addition: Warning
    message: In recycleSingleBracketReplacementValue(value, x, nsbs) :
    number of values supplied is not a sub-multiple of the number of
    values to be replaced
  
  It's probably something really silly but any help would be appreciated ... 
  
  user3828: Is it because rand.gr has not been initialised, so when you ask to set rand.gr[i] it can't because there isn't one yet?
  If you set i to a value (e.g. 1), and run the lines inside your loop, which of the lines produces the error?
  If you don't need to retain all the random genomic ranges at the end, you could just leave out some of the [i]s, something like this:
  for(i in 1:ncol(rand.df)){
    rand.gr <- gr[(elementMetadata(gr)[,"gene"] %in% rand.df[,i])]
    dists <- distanceToNearest(x = rand.gr)
    output <- print(mcols(dists[,1]))
  }
  
  
original_text: 4834: <p>I want to compute the nearest neighbour distances using a for loop. I can do this with a random subset by doing this:</p>
  
  <pre><code>df &lt;-  read.table("tmp.txt", header=F)
  colnames(df) &lt;- c("gene", "seqnames", "start", "stop", "LOCID")
  gr &lt;- makeGRangesFromDataFrame(df, keep.extra.columns=TRUE)
  
  # genes of interest in a list
  gene.list &lt;- read.table("careplastic_diffs.txt")
  
  # filters the GenomicRanges object with genes of interest
  sub.gr &lt;- gr[(elementMetadata(gr)[, "gene"] %in% gene.list$V1)]
  
  # compute distances
  dists &lt;- distanceToNearest(x = sub.gr)
  
  mcols(dists)[,1] # contains a list of distances
  </code></pre>
  
  <p>I now want to essentially do this 5 times, using the same logic as above:</p>
  
  <pre><code>## create a rand dataframe, each column is a random list of 50 genes
  rand.df &lt;- data.frame(replicate(5, sample(gene.list$V1, 50)))
  
  # loop through columns and gets distances and prints to a file
  for(i in 1:ncol(rand.df)){
    rand.gr[i] &lt;- gr[(elementMetadata(gr)[,"gene"] %in% rand.df[,i])]
    dists[i] &lt;- distanceToNearest(x = rand.gr[i])
    output &lt;- print(mcols(dists[i][,1]))
  }
  </code></pre>
  
  <p>But I can't seem to make this work.</p>
  
  <blockquote>
    <p>Error: subscript contains out-of-bounds indices In addition: Warning
    message: In recycleSingleBracketReplacementValue(value, x, nsbs) :<br>
    number of values supplied is not a sub-multiple of the number of
    values to be replaced</p>
  </blockquote>
  
  <p>It's probably something really silly but any help would be appreciated ... </p>
   3828: <p>Is it because rand.gr has not been initialised, so when you ask to set rand.gr[i] it can't because there isn't one yet?</p>
  
  <p>If you set i to a value (e.g. 1), and run the lines inside your loop, which of the lines produces the error?</p>
  
  <p>If you don't need to retain all the random genomic ranges at the end, you could just leave out some of the [i]s, something like this:</p>
  
  <pre><code>for(i in 1:ncol(rand.df)){
    rand.gr &lt;- gr[(elementMetadata(gr)[,"gene"] %in% rand.df[,i])]
    dists &lt;- distanceToNearest(x = rand.gr)
    output &lt;- print(mcols(dists[,1]))
  }
  </code></pre>
  