1
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$
    – llrs
    Nov 3, 2022 at 9:09

2 Answers 2

1
$\begingroup$

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  
   <dbl> <chr>    
 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`
   <chr>     <dbl> <dbl> <dbl>
 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`
  <chr>     <dbl> <dbl> <dbl>
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`
   <chr>     <dbl> <dbl> <dbl>
 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
$\endgroup$
0
$\begingroup$

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
)
$\endgroup$
1
  • 1
    $\begingroup$ Good call @RamRS $\endgroup$
    – M__
    Oct 31, 2022 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.