text: user285: file input file
  a <- read.csv("Studies_studies.tsv",sep = "\t")
  
  #https://stackoverflow.com/questions/43658982/cross-comparison-of-columns-of-the-same-data-frame
  bb <- tcrossprod(table(melt(as.matrix(a))[-1])) * !diag(37)
  
  dd<- setNames(cbind.data.frame(t(combn(names(a), 2)),
                                 combn(names(a), 2, function(x) length(intersect(a[, x[1]],a[, x[2]])))),
                c("col1", "col2", "count"))
  
  So far my code.
  I can get the count between each pairwise intersection happening between the columns but Im not able to get the elements that is common or intersected between two columns.
  For example
  This C0026998 C1879321  1310 I do find that there are 1310 elements common between those to ID but I'm not able to find the name of the ID that are column.
  So that way if I extrapolate my problem what i would like to see is the pairwise intersection among all the elements and i would like to store the output dataframe.
  Any suggestion or help would be really appreciated.
  input file dput
  structure(list(C0026998 = c(32621177L, 32586363L, 32504186L, 
  32391628L, 32333156L, 32171069L), C1879321 = c(27276561L, 29286103L, 
  29988143L, 30076173L, 30024784L, 30892988L), C0023470 = c(27276561L, 
  29988143L, 30076173L, 30024784L, 26837842L, 29286103L), C0280449 = c(30024784L, 
  30076173L, 30892988L, 31688884L, 30651561L, 31516032L), C0522631 = c(26055299L, 
  12801839L, 10516750L, 28825596L, 12652469L, 16203817L), C1292775 = c(28883080L, 
  10400416L, 22581002L, 1503929L, 9499663L, NA), C0023472 = c(26837842L, 
  29091516L, 29498925L, 20525995L, 27217448L, 20525993L), C1292773 = c(30135184L, 
  24440648L, 26273061L, 21508125L, NA, NA), C4545381 = c(30093401L, 
  29933073L, 29437791L, 31227358L, 30541745L, 28555084L), C4707228 = c(26492932L, 
  21791467L, 28179273L, 24077845L, 30287390L, 22362038L), C2939461 = c(28588020L, 
  29988143L, 30076173L, 30510081L, 30024784L, 30963592L), C1336735 = c(28645776L, 
  28844816L, 31400961L, 30545576L, 29241450L, 28588020L), C2827362 = c(29119847L, 
  29486661L, 29567772L, 31775485L, 30670826L, NA), C3839741 = c(28494506L, 
  26492932L, 30923103L, 25715404L, 28556489L, 25006131L), C3839868 = c(28473620L, 
  25512507L, 31015209L, 29933069L, 29321554L, 21791467L), C4520840 = c(28246192L, 
  23935018L, 21750082L, 10741700L, 9407724L, NA), C4528668 = c(28645776L, 
  28588020L, 30510081L, 30409776L, 30093401L, 28844816L), C2825139 = c(29241450L, 
  30541745L, 31152020L, 24663049L, NA, NA), C1332153 = c(24440648L, 
  26273061L, NA, NA, NA, NA), C4721505 = c(18623376L, 21618422L, 
  21685468L, 25196856L, 16985182L, 18353632L), C2826177 = c(27276561L, 
  29286103L, 25512507L, 29079128L, 28473620L, 26789727L), C4324477 = c(29286103L, 
  22375971L, 30153096L, 30368038L, 25006131L, 30054307L), C2826176 = c(27276561L, 
  29988143L, 27959731L, 29286103L, 25512507L, 28588019L), C2827356 = c(27716285L, 
  29177434L, 30952642L, 30418178L, 27126994L, 29656442L), C3711382 = c(30510081L, 
  26492932L, 29991558L, 27927766L, 28490572L, 29860938L), C4704767 = c(30555165L, 
  30055822L, 25540937L, 30135184L, 30545923L, 30361063L), C0027013 = c(22375971L, 
  28962635L, 22375970L, 29515114L, 30185431L, 23216616L), C0153886 = c(30510081L, 
  28644114L, 28588020L, 30555165L, 30024784L, 29304833L), C0279094 = c(28644114L, 
  28588020L, 30510081L, 30555165L, 30076173L, 30024784L), C0279623 = c(27276561L, 
  30555165L, 28588020L, 30076173L, 30024784L, 30510081L), C1142169 = c(28588020L, 
  30510081L, 25512507L, 25540937L, 30409776L, 30668890L), C1275661 = c(29660836L, 
  30963592L, 22579233L, 21880637L, 26772158L, 27121471L), C1292776 = c(30055822L, 
  30115541L, 10025899L, 31152020L, 26577691L, 23129738L), C1320638 = c(27959731L, 
  18489989L, 25425683L, 28179273L, 30510079L, 30655375L), C1737261 = c(29988143L, 
  28645776L, 29933073L, 30545923L, 30361063L, 28336527L), C2826178 = c(25512507L, 
  29286103L, 28473620L, 25611491L, 21242187L, 19059939L), C4744720 = c(30555165L, 
  28644114L, 29597002L, 30541745L, 30118897L, 29660836L), relapse = c(32786187L, 
  32601798L, 32564196L, 32391628L, 32333156L, 32062741L)), row.names = c(NA, 
  6L), class = "data.frame")
  
  output what i have
  dput(head(dd))
  structure(list(col1 = structure(c(3L, 3L, 3L, 3L, 3L, 3L), .Label = c("C0023470", 
  "C0023472", "C0026998", "C0027013", "C0153886", "C0279094", "C0279623", 
  "C0280449", "C0522631", "C1142169", "C1275661", "C1292773", "C1292775", 
  "C1292776", "C1320638", "C1332153", "C1336735", "C1737261", "C1879321", 
  "C2825139", "C2826176", "C2826177", "C2826178", "C2827356", "C2827362", 
  "C2939461", "C3711382", "C3839741", "C3839868", "C4324477", "C4520840", 
  "C4528668", "C4545381", "C4704767", "C4707228", "C4721505", "C4744720"
  ), class = "factor"), col2 = structure(c(18L, 1L, 7L, 8L, 12L, 
  2L), .Label = c("C0023470", "C0023472", "C0027013", "C0153886", 
  "C0279094", "C0279623", "C0280449", "C0522631", "C1142169", "C1275661", 
  "C1292773", "C1292775", "C1292776", "C1320638", "C1332153", "C1336735", 
  "C1737261", "C1879321", "C2825139", "C2826176", "C2826177", "C2826178", 
  "C2827356", "C2827362", "C2939461", "C3711382", "C3839741", "C3839868", 
  "C4324477", "C4520840", "C4528668", "C4545381", "C4704767", "C4707228", 
  "C4721505", "C4744720", "relapse"), class = "factor"), count = structure(c(1310L, 
  1310L, 164L, 9L, 6L, 23L), .Dim = 6L)), row.names = c(NA, 6L), class = "data.frame")
  
  from the above output i do know that  C0026998 C1879321  1310 between C0026998 and C1879321 there are 1310 elements are common. Now the next thing i would like to find out is what are the elements that are common between those two IDs same goes for rest of the column which are to be compared pairwise.
  The output I'm expecting is this
  structure(list(`intersect(a$C0026998, a$C1879321)` = c(32621177L, 
  32586363L, 32504186L, 32391628L, 32333156L, 32171069L)), row.names = c(NA, 
  6L), class = "data.frame")
  
  
  structure(list(`intersect(a$C0026998, a$C0280449)` = c(32171069L, 
  31841594L, 31770437L, 31753773L, 31688884L, 31516032L)), row.names = c(NA, 
  6L), class = "data.frame")
  
  So in each column contain one Mesh term where there PMIDS which i want to find if these studies are overlapping or not.So in the above example i have given intersection between first four columns so i can see the IDs which are common. So i want to do the same with all the columns and save the results.
  
  user9471: A tidyverse answer.
  library("tidyverse")
  
  results <- df %>%
    {gtools::combinations(ncol(.), 2, colnames(.))} %>%
    as_tibble(.name_repair="unique") %>%
    rename(col1=1, col2=2) %>%
    rowwise %>%
    mutate(intersection=list(intersect({{df}}[[col1]], {{df}}[[col2]]))) %>%
    unnest_wider(col=intersection, names_repair="unique") %>%
    pivot_longer(starts_with("..."), names_to="col", values_to="val") %>%
    pivot_wider(names_from=c(col1, col2), names_sep="_", values_from=val) %>%
    select(!col)
  
  The first few results.
  > results[1:5, 1:5]
  # A tibble: 5 x 5
    C0023470_C00234~ C0023470_C00269~ C0023470_C00270~ C0023470_C01538~
               <int>            <int>            <int>            <int>
  1         26837842               NA               NA         30024784
  2               NA               NA               NA               NA
  3               NA               NA               NA               NA
  4               NA               NA               NA               NA
  5               NA               NA               NA               NA
  # ... with 1 more variable: C0023470_C0279094 <int>
  
  
original_text: 285: <p><a href="https://drive.google.com/file/d/1sUjrKaPgqJRixmGoWrKfNo1WYOA_qloj/view?usp=sharing" rel="nofollow noreferrer">file</a> input file</p>
  <pre><code>a &lt;- read.csv(&quot;Studies_studies.tsv&quot;,sep = &quot;\t&quot;)
  
  #https://stackoverflow.com/questions/43658982/cross-comparison-of-columns-of-the-same-data-frame
  bb &lt;- tcrossprod(table(melt(as.matrix(a))[-1])) * !diag(37)
  
  dd&lt;- setNames(cbind.data.frame(t(combn(names(a), 2)),
                                 combn(names(a), 2, function(x) length(intersect(a[, x[1]],a[, x[2]])))),
                c(&quot;col1&quot;, &quot;col2&quot;, &quot;count&quot;))
  </code></pre>
  <p>So far my code.</p>
  <p>I can get the count between each pairwise intersection happening between the columns but Im not able to get the elements that is common or intersected between two columns.</p>
  <p><strong>For example</strong><br />
  This <code>C0026998 C1879321  1310</code> I do find that there are 1310 elements common between those to ID but I'm not able to find the name of the ID that are column.</p>
  <p>So that way if I extrapolate my problem what i would like to see is the pairwise intersection among all the elements and i would like to store the output dataframe.</p>
  <p>Any suggestion or help would be really appreciated.</p>
  <p>input file dput</p>
  <pre><code>structure(list(C0026998 = c(32621177L, 32586363L, 32504186L, 
  32391628L, 32333156L, 32171069L), C1879321 = c(27276561L, 29286103L, 
  29988143L, 30076173L, 30024784L, 30892988L), C0023470 = c(27276561L, 
  29988143L, 30076173L, 30024784L, 26837842L, 29286103L), C0280449 = c(30024784L, 
  30076173L, 30892988L, 31688884L, 30651561L, 31516032L), C0522631 = c(26055299L, 
  12801839L, 10516750L, 28825596L, 12652469L, 16203817L), C1292775 = c(28883080L, 
  10400416L, 22581002L, 1503929L, 9499663L, NA), C0023472 = c(26837842L, 
  29091516L, 29498925L, 20525995L, 27217448L, 20525993L), C1292773 = c(30135184L, 
  24440648L, 26273061L, 21508125L, NA, NA), C4545381 = c(30093401L, 
  29933073L, 29437791L, 31227358L, 30541745L, 28555084L), C4707228 = c(26492932L, 
  21791467L, 28179273L, 24077845L, 30287390L, 22362038L), C2939461 = c(28588020L, 
  29988143L, 30076173L, 30510081L, 30024784L, 30963592L), C1336735 = c(28645776L, 
  28844816L, 31400961L, 30545576L, 29241450L, 28588020L), C2827362 = c(29119847L, 
  29486661L, 29567772L, 31775485L, 30670826L, NA), C3839741 = c(28494506L, 
  26492932L, 30923103L, 25715404L, 28556489L, 25006131L), C3839868 = c(28473620L, 
  25512507L, 31015209L, 29933069L, 29321554L, 21791467L), C4520840 = c(28246192L, 
  23935018L, 21750082L, 10741700L, 9407724L, NA), C4528668 = c(28645776L, 
  28588020L, 30510081L, 30409776L, 30093401L, 28844816L), C2825139 = c(29241450L, 
  30541745L, 31152020L, 24663049L, NA, NA), C1332153 = c(24440648L, 
  26273061L, NA, NA, NA, NA), C4721505 = c(18623376L, 21618422L, 
  21685468L, 25196856L, 16985182L, 18353632L), C2826177 = c(27276561L, 
  29286103L, 25512507L, 29079128L, 28473620L, 26789727L), C4324477 = c(29286103L, 
  22375971L, 30153096L, 30368038L, 25006131L, 30054307L), C2826176 = c(27276561L, 
  29988143L, 27959731L, 29286103L, 25512507L, 28588019L), C2827356 = c(27716285L, 
  29177434L, 30952642L, 30418178L, 27126994L, 29656442L), C3711382 = c(30510081L, 
  26492932L, 29991558L, 27927766L, 28490572L, 29860938L), C4704767 = c(30555165L, 
  30055822L, 25540937L, 30135184L, 30545923L, 30361063L), C0027013 = c(22375971L, 
  28962635L, 22375970L, 29515114L, 30185431L, 23216616L), C0153886 = c(30510081L, 
  28644114L, 28588020L, 30555165L, 30024784L, 29304833L), C0279094 = c(28644114L, 
  28588020L, 30510081L, 30555165L, 30076173L, 30024784L), C0279623 = c(27276561L, 
  30555165L, 28588020L, 30076173L, 30024784L, 30510081L), C1142169 = c(28588020L, 
  30510081L, 25512507L, 25540937L, 30409776L, 30668890L), C1275661 = c(29660836L, 
  30963592L, 22579233L, 21880637L, 26772158L, 27121471L), C1292776 = c(30055822L, 
  30115541L, 10025899L, 31152020L, 26577691L, 23129738L), C1320638 = c(27959731L, 
  18489989L, 25425683L, 28179273L, 30510079L, 30655375L), C1737261 = c(29988143L, 
  28645776L, 29933073L, 30545923L, 30361063L, 28336527L), C2826178 = c(25512507L, 
  29286103L, 28473620L, 25611491L, 21242187L, 19059939L), C4744720 = c(30555165L, 
  28644114L, 29597002L, 30541745L, 30118897L, 29660836L), relapse = c(32786187L, 
  32601798L, 32564196L, 32391628L, 32333156L, 32062741L)), row.names = c(NA, 
  6L), class = &quot;data.frame&quot;)
  </code></pre>
  <p>output what i have</p>
  <pre><code>dput(head(dd))
  structure(list(col1 = structure(c(3L, 3L, 3L, 3L, 3L, 3L), .Label = c(&quot;C0023470&quot;, 
  &quot;C0023472&quot;, &quot;C0026998&quot;, &quot;C0027013&quot;, &quot;C0153886&quot;, &quot;C0279094&quot;, &quot;C0279623&quot;, 
  &quot;C0280449&quot;, &quot;C0522631&quot;, &quot;C1142169&quot;, &quot;C1275661&quot;, &quot;C1292773&quot;, &quot;C1292775&quot;, 
  &quot;C1292776&quot;, &quot;C1320638&quot;, &quot;C1332153&quot;, &quot;C1336735&quot;, &quot;C1737261&quot;, &quot;C1879321&quot;, 
  &quot;C2825139&quot;, &quot;C2826176&quot;, &quot;C2826177&quot;, &quot;C2826178&quot;, &quot;C2827356&quot;, &quot;C2827362&quot;, 
  &quot;C2939461&quot;, &quot;C3711382&quot;, &quot;C3839741&quot;, &quot;C3839868&quot;, &quot;C4324477&quot;, &quot;C4520840&quot;, 
  &quot;C4528668&quot;, &quot;C4545381&quot;, &quot;C4704767&quot;, &quot;C4707228&quot;, &quot;C4721505&quot;, &quot;C4744720&quot;
  ), class = &quot;factor&quot;), col2 = structure(c(18L, 1L, 7L, 8L, 12L, 
  2L), .Label = c(&quot;C0023470&quot;, &quot;C0023472&quot;, &quot;C0027013&quot;, &quot;C0153886&quot;, 
  &quot;C0279094&quot;, &quot;C0279623&quot;, &quot;C0280449&quot;, &quot;C0522631&quot;, &quot;C1142169&quot;, &quot;C1275661&quot;, 
  &quot;C1292773&quot;, &quot;C1292775&quot;, &quot;C1292776&quot;, &quot;C1320638&quot;, &quot;C1332153&quot;, &quot;C1336735&quot;, 
  &quot;C1737261&quot;, &quot;C1879321&quot;, &quot;C2825139&quot;, &quot;C2826176&quot;, &quot;C2826177&quot;, &quot;C2826178&quot;, 
  &quot;C2827356&quot;, &quot;C2827362&quot;, &quot;C2939461&quot;, &quot;C3711382&quot;, &quot;C3839741&quot;, &quot;C3839868&quot;, 
  &quot;C4324477&quot;, &quot;C4520840&quot;, &quot;C4528668&quot;, &quot;C4545381&quot;, &quot;C4704767&quot;, &quot;C4707228&quot;, 
  &quot;C4721505&quot;, &quot;C4744720&quot;, &quot;relapse&quot;), class = &quot;factor&quot;), count = structure(c(1310L, 
  1310L, 164L, 9L, 6L, 23L), .Dim = 6L)), row.names = c(NA, 6L), class = &quot;data.frame&quot;)
  </code></pre>
  <p>from the above output i do know that  <code>C0026998 C1879321  1310</code> between C0026998 and C1879321 there are 1310 elements are common. Now the next thing i would like to find out is what are the elements that are common between those two IDs same goes for rest of the column which are to be compared pairwise.</p>
  <p>The output I'm expecting is this</p>
  <pre><code>structure(list(`intersect(a$C0026998, a$C1879321)` = c(32621177L, 
  32586363L, 32504186L, 32391628L, 32333156L, 32171069L)), row.names = c(NA, 
  6L), class = &quot;data.frame&quot;)
  
  
  structure(list(`intersect(a$C0026998, a$C0280449)` = c(32171069L, 
  31841594L, 31770437L, 31753773L, 31688884L, 31516032L)), row.names = c(NA, 
  6L), class = &quot;data.frame&quot;)
  </code></pre>
  <p>So in each column contain one Mesh term where there PMIDS which i want to find if these studies are overlapping or not.So in the above example i have given intersection between first four columns so i can see the IDs which are common. So i want to do the same with all the columns and save the results.</p>
   9471: <p>A tidyverse answer.</p>
  <pre><code>library(&quot;tidyverse&quot;)
  
  results &lt;- df %&gt;%
    {gtools::combinations(ncol(.), 2, colnames(.))} %&gt;%
    as_tibble(.name_repair=&quot;unique&quot;) %&gt;%
    rename(col1=1, col2=2) %&gt;%
    rowwise %&gt;%
    mutate(intersection=list(intersect({{df}}[[col1]], {{df}}[[col2]]))) %&gt;%
    unnest_wider(col=intersection, names_repair=&quot;unique&quot;) %&gt;%
    pivot_longer(starts_with(&quot;...&quot;), names_to=&quot;col&quot;, values_to=&quot;val&quot;) %&gt;%
    pivot_wider(names_from=c(col1, col2), names_sep=&quot;_&quot;, values_from=val) %&gt;%
    select(!col)
  </code></pre>
  <p>The first few results.</p>
  <pre><code>&gt; results[1:5, 1:5]
  # A tibble: 5 x 5
    C0023470_C00234~ C0023470_C00269~ C0023470_C00270~ C0023470_C01538~
               &lt;int&gt;            &lt;int&gt;            &lt;int&gt;            &lt;int&gt;
  1         26837842               NA               NA         30024784
  2               NA               NA               NA               NA
  3               NA               NA               NA               NA
  4               NA               NA               NA               NA
  5               NA               NA               NA               NA
  # ... with 1 more variable: C0023470_C0279094 &lt;int&gt;
  </code></pre>
  