<p>When I create the dataframe using the commands you have provided, I get an error, and the data frame can't be created:</p>
<pre><code>Error in data.frame(Pathways_1Day, Pathways_2Day, Pathways_3Day) : 
  arguments imply differing number of rows: 12, 11
</code></pre>
<p>What I <em>think</em> you were trying to do is something more like this:</p>
<pre><code>library(tidyverse)
tibble() %&gt;% 
  bind_rows(tibble(day=1, pathway = Pathways_1Day)) %&gt;%
  bind_rows(tibble(day=2, pathway = Pathways_2Day)) %&gt;%
  bind_rows(tibble(day=3, pathway = Pathways_3Day)) -&gt; combined.tbl
</code></pre>
<p>Producing a long-format data structure that looks like this:</p>
<pre><code>&gt; combined.tbl
# A tibble: 34 x 2
     day pathway  
   &lt;dbl&gt; &lt;chr&gt;    
 1     1 blood    
 2     1 kidney   
 3     1 testis   
 4     1 No       
 5     1 bone     
 6     1 liver    
 7     1 intestine
 8     1 lungs    
 9     1 ABC      
10     1 pancreas 
# ... with 24 more rows
</code></pre>
<p>When laid out in this way, you can do something like grouping by pathway, and then do a <code>pivot_wider</code> to show the sets:</p>
<pre><code>&gt; combined.tbl %&gt;% 
     group_by(pathway) %&gt;%
     arrange(pathway) %&gt;%
     pivot_wider(names_from=day, values_from=1)
# A tibble: 20 x 4
# Groups:   pathway [20]
   pathway     `1`   `2`   `3`
   &lt;chr&gt;     &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
 1 ABC           1     2     3
 2 No            1    NA    NA
 3 None         NA     2     3
 4 Yes           1    NA    NA
 5 blood         1     2     3
 6 bone          1     2    NA
 7 cells        NA     2    NA
 8 cervix       NA     2    NA
 9 epidermis    NA    NA     3
10 eyes         NA     2    NA
11 intestine     1     2     3
12 kidney        1     2     3
13 liver         1    NA     3
14 lungs         1    NA     3
15 lymph        NA    NA     3
16 pancreas      1     2    NA
17 spleen        1    NA    NA
18 t-cells      NA    NA     3
19 testis        1     2    NA
20 vessels      NA    NA     3
</code></pre>
<p>And insert a filter to select only pathways that appear in all three days (i.e. <code>n() == 3</code>):</p>
<pre><code>&gt; combined.tbl %&gt;% 
     group_by(pathway) %&gt;%
     filter(n() == 3) %&gt;%
     arrange(pathway) %&gt;%
     pivot_wider(names_from=day, values_from=1)
# A tibble: 4 x 4
# Groups:   pathway [4]
  pathway     `1`   `2`   `3`
  &lt;chr&gt;     &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1 ABC           1     2     3
2 blood         1     2     3
3 intestine     1     2     3
4 kidney        1     2     3
</code></pre>
<p>Or at least two days (<code>n() &gt;= 2</code>):</p>
<pre><code>&gt; combined.tbl %&gt;% 
     group_by(pathway) %&gt;%
     filter(n() &gt;= 2) %&gt;%
     arrange(pathway) %&gt;%
     pivot_wider(names_from=day, values_from=1)
# A tibble: 10 x 4
# Groups:   pathway [10]
   pathway     `1`   `2`   `3`
   &lt;chr&gt;     &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
 1 ABC           1     2     3
 2 None         NA     2     3
 3 blood         1     2     3
 4 bone          1     2    NA
 5 intestine     1     2     3
 6 kidney        1     2     3
 7 liver         1    NA     3
 8 lungs         1    NA     3
 9 pancreas      1     2    NA
10 testis        1     2    NA
</code></pre>

CC BY-SA 4.0
2022-11-02T10:25:00.133Z
2022-11-02T22:45:32.943Z
2022-11-02T22:45:32.943Z
gringer
<p>You're misusing data frames. Split the df into 4 vectors and use a Venn diagram generator to denerate a Venn diagram with the 4 vectors.</p>
<p>Adapting the first example from <a href="https://r-graph-gallery.com/14-venn-diagramm" rel="nofollow noreferrer">here</a></p>
<pre class="lang-r prettyprint-override"><code>#untested
venn.diagram(
  x = list(df[,1], df[,2], df[,3], df[,4]), 
  category.names = colnames(df),
  filename = 'my_venn_diagram.png',
  output=TRUE
)
</code></pre>

CC BY-SA 4.0
2022-10-31T23:25:16.550Z
Good call @RamRS
M__
CC BY-SA 4.0
2022-10-31T21:59:35.563Z
2022-10-31T21:59:35.563Z
Ram RS
<p>I am working on a large data-frame that has 4 columns and each column has variable rows. I am trying to find unique and identical pathways between the 4 columns (each column represents a particular day of treatment with a drug). Here below is a small example.</p>
<pre><code>Pathways_1Day &lt;- c(&quot;blood&quot;,&quot;kidney&quot;,&quot;testis&quot;,&quot;No&quot;,&quot;bone&quot;,&quot;liver&quot;,&quot;intestine&quot;,&quot;lungs&quot;,&quot;ABC&quot;,&quot;pancreas&quot;,&quot;Yes&quot;,&quot;spleen&quot;)

Pathways_2Day &lt;- c(&quot;blood&quot;,&quot;kidney&quot;,&quot;testis&quot;,&quot;eyes&quot;,&quot;bone&quot;,&quot;cells&quot;,&quot;intestine&quot;,&quot;cervix&quot;,&quot;ABC&quot;,&quot;pancreas&quot;,&quot;None&quot;)

Pathways_3Day &lt;- c(&quot;blood&quot;,&quot;kidney&quot;,&quot;vessels&quot;,&quot;lymph&quot;,&quot;t-cells&quot;,&quot;liver&quot;,&quot;intestine&quot;,&quot;lungs&quot;,&quot;ABC&quot;,&quot;epidermis&quot;,&quot;None&quot;)

df&lt;-data.frame(Pathways_1Day,Pathways_2Day,Pathways_3Day) 
</code></pre>
<p>I want to get a summary of the no of pathways that are common between different timepoints (1, 2 and 3 days).</p>
<p><strong>Important: The no of pathways is not the same for each day.</strong></p>
<p>I have tried this:</p>
<pre><code>All_pathwayNames &lt;- df%&gt;%group_by_all%&gt;%count
</code></pre>
<p>But the desired output is not what I am trying to get.</p>
<p>There can be different ways to address that. It will be great if I can get matching rows infront of each other across all columns.</p>
<p>Regards</p>

CC BY-SA 4.0
2022-11-03T09:09:40.770Z
Are pathways on different day with the same genes? Then It is just a matter to compare the names of each day via `setdiff(Pathways_3Day, Pathways_1Day)` for example. If not I would recommend it to work with lists instead of data.frame for this kind of data or use GeneSetCollection from GSEABase to read it and work with this.
llrs
<r><dataframe>
How to identify matching and unique rows across different columns in R data-frame?
# How to identify matching and unique rows across different columns in R data-frame?

(Asked by: username_0 on 2022-10-31)

I am working on a large data-frame that has 4 columns and each column has variable rows. I am trying to find unique and identical pathways between the 4 columns (each column represents a particular day of treatment with a drug). Here below is a small example.
Pathways_1Day <- c("blood","kidney","testis","No","bone","liver","intestine","lungs","ABC","pancreas","Yes","spleen")

Pathways_2Day <- c("blood","kidney","testis","eyes","bone","cells","intestine","cervix","ABC","pancreas","None")

Pathways_3Day <- c("blood","kidney","vessels","lymph","t-cells","liver","intestine","lungs","ABC","epidermis","None")

df<-data.frame(Pathways_1Day,Pathways_2Day,Pathways_3Day) 

I want to get a summary of the no of pathways that are common between different timepoints (1, 2 and 3 days).
Important: The no of pathways is not the same for each day.
I have tried this:
All_pathwayNames <- df%>%group_by_all%>%count

But the desired output is not what I am trying to get.
There can be different ways to address that. It will be great if I can get matching rows infront of each other across all columns.
Regards

**Tags:** r, dataframe
> **Comments:**
> **username_1**: Are pathways on different day with the same genes? Then It is just a matter to compare the names of each day via `setdiff(Pathways_3Day, Pathways_1Day)` for example. If not I would recommend it to work with lists instead of data.frame for this kind of data or use GeneSetCollection from GSEABase to read it and work with this.  

---

## 2 answers

### Answer by username_2 on 2022-11-02. Score: 1

When I create the dataframe using the commands you have provided, I get an error, and the data frame can't be created:
Error in data.frame(Pathways_1Day, Pathways_2Day, Pathways_3Day) : 
  arguments imply differing number of rows: 12, 11

What I think you were trying to do is something more like this:
library(tidyverse)
tibble() %>% 
  bind_rows(tibble(day=1, pathway = Pathways_1Day)) %>%
  bind_rows(tibble(day=2, pathway = Pathways_2Day)) %>%
  bind_rows(tibble(day=3, pathway = Pathways_3Day)) -> combined.tbl

Producing a long-format data structure that looks like this:
> combined.tbl
# A tibble: 34 x 2
     day pathway  
        
 1     1 blood    
 2     1 kidney   
 3     1 testis   
 4     1 No       
 5     1 bone     
 6     1 liver    
 7     1 intestine
 8     1 lungs    
 9     1 ABC      
10     1 pancreas 
# ... with 24 more rows

When laid out in this way, you can do something like grouping by pathway, and then do a pivot_wider to show the sets:
> combined.tbl %>% 
     group_by(pathway) %>%
     arrange(pathway) %>%
     pivot_wider(names_from=day, values_from=1)
# A tibble: 20 x 4
# Groups:   pathway [20]
   pathway     `1`   `2`   `3`
          
 1 ABC           1     2     3
 2 No            1    NA    NA
 3 None         NA     2     3
 4 Yes           1    NA    NA
 5 blood         1     2     3
 6 bone          1     2    NA
 7 cells        NA     2    NA
 8 cervix       NA     2    NA
 9 epidermis    NA    NA     3
10 eyes         NA     2    NA
11 intestine     1     2     3
12 kidney        1     2     3
13 liver         1    NA     3
14 lungs         1    NA     3
15 lymph        NA    NA     3
16 pancreas      1     2    NA
17 spleen        1    NA    NA
18 t-cells      NA    NA     3
19 testis        1     2    NA
20 vessels      NA    NA     3

And insert a filter to select only pathways that appear in all three days (i.e. n() == 3):
> combined.tbl %>% 
     group_by(pathway) %>%
     filter(n() == 3) %>%
     arrange(pathway) %>%
     pivot_wider(names_from=day, values_from=1)
# A tibble: 4 x 4
# Groups:   pathway [4]
  pathway     `1`   `2`   `3`
         
1 ABC           1     2     3
2 blood         1     2     3
3 intestine     1     2     3
4 kidney        1     2     3

Or at least two days (n() >= 2):
> combined.tbl %>% 
     group_by(pathway) %>%
     filter(n() >= 2) %>%
     arrange(pathway) %>%
     pivot_wider(names_from=day, values_from=1)
# A tibble: 10 x 4
# Groups:   pathway [10]
   pathway     `1`   `2`   `3`
          
 1 ABC           1     2     3
 2 None         NA     2     3
 3 blood         1     2     3
 4 bone          1     2    NA
 5 intestine     1     2     3
 6 kidney        1     2     3
 7 liver         1    NA     3
 8 lungs         1    NA     3
 9 pancreas      1     2    NA
10 testis        1     2    NA


---

### Answer by username_3 on 2022-10-31. Score: 0

You're misusing data frames. Split the df into 4 vectors and use a Venn diagram generator to denerate a Venn diagram with the 4 vectors.
Adapting the first example from here
#untested
venn.diagram(
  x = list(df[,1], df[,2], df[,3], df[,4]), 
  category.names = colnames(df),
  filename = 'my_venn_diagram.png',
  output=TRUE
)

> **Comments:**
> **username_4**: Good call @RamRS  

---

