title: How can I compare two lists and output "hits" into a dataframe
body: <p>I've tried to find answers here and on google but no luck, been struggling with this issue for some days so would really appreciate help. I'm analyzing a network to see if cycles tend to be within discreet communities or between them, or no pattern. My data are a list of cycles (three nodes forming a loop) and a list of communities (variable amount of nodes). I have two questions, 1) how to compare two lists, and 2) how to output the comparison results in a way which is readable:</p>
  
  <ul>
  <li>Question 1</li>
  </ul>
  
  <p>I have two lists (both igraph objects), one containing 678 items (each of 3 elements, all characters) and another containing 11 items each with a differing number of elements. Example:</p>
  
  <pre><code>x1 &lt;- as.character(c(1,3,5))
  x2 &lt;- as.character(c(2,4,6))
  x3 &lt;- as.character(c(7,8,9))
  x4 &lt;- as.character(c(10,11,12))
  x &lt;- list(x1, x2, x3, x4)
  
  y1 &lt;- as.character(c(1,2,3,4,5))
  y2 &lt;- as.character(c(2,3,4,5))
  y3 &lt;- as.character(c(1,2,3,4,5,7,8,9))
  y &lt;- list(y1, y2, y3)
  </code></pre>
  
  <p>Giving:</p>
  
  <pre><code>&gt; x
  [[1]]
  [1] "1" "3" "5"
  
  [[2]]
  [1] "2" "4" "6"
  
  [[3]]
  [1] "7" "8" "9"
  
  [[4]]
  [1] "10" "11" "12"
  
  &gt; y
  [[1]]
  [1] "1" "2" "3" "4" "5"
  
  [[2]]
  [1] "2" "3" "4" "5"
  
  [[3]]
  [1] "1" "2" "3" "4" "5" "7" "8" "9"
  </code></pre>
  
  <p>I want to compare every component in <code>x</code> against every component in <code>y</code> and add every hit (i.e. when all the elements from <code>x[[i]]</code> are also found in <code>y[[i]]</code>) to a new dataframe. I tried a loop using <code>all()</code> and <code>%in%</code> but this didn't work:</p>
  
  <pre><code>for (i in 1:length(x)) {
    for (j in 1:length(y)) {
      hits &lt;- all(y[[j]] %in% x[[i]]) == TRUE
      print(hits)
        }
      }
  </code></pre>
  
  <p>This returns 12 <code>FALSE</code> hits. Checking individual components, it should have worked, because:</p>
  
  <pre><code>all(x[[1]] %in% y[[1]])
  </code></pre>
  
  <p>Returns <code>TRUE</code> as it should, and:</p>
  
  <pre><code>all(x[[1]] %in% y[[2]])
  </code></pre>
  
  <p>Returns <code>FALSE</code> as it should. Where am I going wrong here?</p>
  
  <ul>
  <li>Question 2</li>
  </ul>
  
  <p>I have seen some solutions for outputting loop results into a df, but that's not exactly what I need. What I want as an output is a dataframe telling me which community every cycle is in. Since there's only 11 communities, it could just refer me to the list component's index, but I haven't found a way to do this. I could also just use <code>paste()</code> to concatenate the node names of a community into a title. Either way, here is the output I need:</p>
  
  <pre><code>  cycle       community
  1 1_3_5       1_2_3_4_5
  2 1_3_5 1_2_3_4_5_7_8_9
  3 7_8_9 1_2_3_4_5_7_8_9
  </code></pre>
  
  <p>I'm guessing some kind of an if statement. I feel this should be fairly simple to execute and that I should have been able to work it out myself. Nevertheless, thank you for your time and sorry about the essay.</p>