title: Finding intersection entries in a data frame
body: <p>I've run into an R-programming problem that I can't seem to wrap my head around.  I have data like the following:</p>
  
  <pre><code>data = data.frame("start"=c(1,2,4,5),
                    "length"=c(2,2,2,3),
                    "decision"=c("yes","no","yes","yes"))
  </code></pre>
  
  <p>Which looks like:</p>
  
  <pre><code>  start length decision
  1     1      2      yes
  2     2      2       no
  3     4      2      yes
  4     5      3      yes
  </code></pre>
  
  <p>Row one stands for a sequence of integers that start at 1 for length 2 (1,2).  Row 3 is 2 integers starting at 4 (4,5).  I'm looking for intersections between entries that have a 'yes' decision variable.  When the decision variable is 'no', then the sequence is thrown out.  Here's what I've attempted so far.</p>
  
  <p>I think I need to create a sequence list first.</p>
  
  <pre><code>sequence.list = lapply(seq(dim(data)[1]),
                         function(d){
                           seq(data$start[d],(data$start[d]+data$length[d]-1),by=1)
                           })
  </code></pre>
  
  <p>This outputs:</p>
  
  <pre><code>sequence.list
  [[1]]
  [1] 1 2
  
  [[2]]
  [1] 2 3
  
  [[3]]
  [1] 4 5
  
  [[4]]
  [1] 5 6 7
  </code></pre>
  
  <p>Which is a start.  Then I create a list that counts intersections between items on my list (I stole this idea from another post on here).</p>
  
  <pre><code>count.intersect = lapply(sequence.list,function(a) {
    sapply(seq(length(sequence.list)), 
           function(b) length(intersect(sequence.list[[b]], a)))
    })
  </code></pre>
  
  <p>This creates the list:</p>
  
  <pre><code> count.intersect
  [[1]]
  [1] 2 1 0 0
  
  [[2]]
  [1] 1 2 0 0
  
  [[3]]
  [1] 0 0 2 1
  
  [[4]]
  [1] 0 0 1 3
  </code></pre>
  
  <p>The way to read this is that entry 1 in the data frame has 2 trivial intersections with itself and 1 intersection with entry 2.</p>
  
  <p>Here's where I get fuzzy on what to do.  Make it a matrix?</p>
  
  <pre><code>intersect.matrix = do.call(rbind,count.intersect)
  </code></pre>
  
  <p>Then set the rows and columns of non-used entries to zero?</p>
  
  <pre><code>intersect.matrix[,data$decision=="no"]=0
  intersect.matrix[data$decision=="no",]=0
  
  intersect.matrix
       [,1] [,2] [,3] [,4]
  [1,]    2    0    0    0
  [2,]    0    0    0    0
  [3,]    0    0    2    1
  [4,]    0    0    1    3
  </code></pre>
  
  <p>Now, I would like to return indices 3 and 4 somehow.  I want to find the rows (or columns) containing non zeros that are also not on the diagonal.</p>
  
  <p>Sorry for posting the whole procedure, I also want to know if there is a shorter way to go from the starting dataframe to finding intersections in used entries.</p>