Answers.Body: <p>To make a confusion matrix of 4 lists against 4 other lists, you need to get the length of all intersecting genes for each pair. Here an example with letters instead of gene names.</p>
  
  <pre><code>set.seed(11)
  list1_up &lt;- sample(letters[1:26], 20)
  list1_down &lt;- sample(letters[1:26], 20)
  list2_up &lt;- sample(letters[1:26], 20)
  list2_down &lt;- sample(letters[1:26], 20)
  list3_up &lt;- sample(letters[1:26], 20)
  list3_down &lt;- sample(letters[1:26], 20)
  list4_up &lt;- sample(letters[1:26], 20)
  list4_down &lt;- sample(letters[1:26], 20)
  
  cross_table &lt;- matrix(, nrow = 4, ncol = 4)
  rownames(cross_table) &lt;- c("list1_up", "list2_up", "list3_up", "list4_up")
  colnames(cross_table) &lt;- c("list1_down", "list2_down", "list3_down", "list4_down")
  
  for (i in 1:4){
    for(j in 1:4){
      cross_table[i,j] &lt;- length(intersect(get(paste0("list",i,"_up")),(get(paste0("list",j,"_down")))))
      }
  } 
  
  cross_table
           list1_down list2_down list3_down list4_down
  list1_up         15         14         15         15
  list2_up         18         15         15         15
  list3_up         17         15         14         16
  list4_up         17         15         15         16
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-30T00:26:32.617Z
Answers.Text: Please could you look at my attempt at altering this code to suit my data and see where I have gone wrong? I have added the code to my question above
Answers.UserDisplayName: user3762
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-30T07:51:19.340Z
Answers.Text: No, I will not look at your attempt. Here is my suggestion, do with it what you want.
Answers.UserDisplayName: benn
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-30T08:18:37.280Z
Answers.Text: @user3762 It would be better if you describe what has gone wrong and how the solution here fails you. Also show your attempts to fix or adapt this solution into one that works for you. Good luck!
Answers.UserDisplayName: llrs
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-05T23:10:00.207Z
Answers.Text: @benn nice manners
Answers.UserDisplayName: user3479780
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-29T16:10:25.900Z
Answers.LastActivityDate: 2018-11-29T20:12:03.087Z
Answers.LastEditDate: 2018-11-29T20:12:03.087Z
Answers.UserDisplayName: benn
Body: <p>I am looking to create a table (on RStudio) that will show the overlap between 8 different gene lists wherever possible. I have saved the 8 lists as .txt files all in one folder and set that particular folder as my working directory. Any idea of code that will work for this?</p>
  
  <p><a href="https://i.sstatic.net/Pn9A1.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Pn9A1.png" alt="Output table with NA values"></a></p>
  
  <p>If anyone knows what I can alter to make the current code work or suggest an alternative way I would be very happy to hear any suggestions!</p>
  
  <pre><code>data.file1 &lt;- "GSE108363_BCGdown_V.txt"
  data.file2 &lt;- "GSE108363_BCGdown_D.txt"
  data.file3 &lt;- "GSE108363_BCGup_V.txt"
  data.file4 &lt;- "GSE108363_BCGup_D.txt"
  data.file5 &lt;- "GSE108363_MTBdown_V.txt"
  data.file6 &lt;- "GSE108363_MTBdown_D.txt"
  data.file7 &lt;- "GSE108363_MTBup_V.txt"
  data.file8 &lt;- "GSE108363_MTBup_D.txt"
  
  genevect1 &lt;- scan(data.file1, what=character(), sep="\n")
  genevect2 &lt;- scan(data.file2, what=character(), sep="\n")
  genevect3 &lt;- scan(data.file3, what=character(), sep="\n")
  genevect4 &lt;- scan(data.file4, what=character(), sep="\n")
  genevect5 &lt;- scan(data.file5, what=character(), sep="\n")
  genevect6 &lt;- scan(data.file6, what=character(), sep="\n")
  genevect7 &lt;- scan(data.file7, what=character(), sep="\n")
  genevect8 &lt;- scan(data.file8, what=character(), sep="\n")
  
  
  filelist &lt;- list(data.file1, data.file2, data.file3, data.file4, data.file5, data.file6, data.file7, data.file8)
  #-------------------------------------------------------------------------------------------------------
  # "Lapply" is basically making a "for loop".
  #-------------------------------------------------------------------------------------------------------
  all_gene_vectors &lt;- lapply(filelist, scan, what=character(), sep="\n")
  
  #-------------------------------------------------------------------------------------------------------
  # Then make the intersection of *all* genesets
  #-------------------------------------------------------------------------------------------------------
  final_inter &lt;- all_gene_vectors[[1]]
  for (next_genevect in all_gene_vectors[2:length(all_gene_vectors)]) {
    final_inter &lt;- intersect(final_inter, next_genevect)
  }
  show(final_inter)
  
  #-------------------------------------------------------------------------------------------------------
  # Or compute all pairwise intersections (untested)
  #-------------------------------------------------------------------------------------------------------
  pairwise_inters &lt;- list()
  #-------------------------------------------------------------------------------------------------------
  # Iterate over all possible pairs i,j (i ≠ j)
  #-------------------------------------------------------------------------------------------------------
  N &lt;- length(all_gene_vectors)
  for (i in 1:(N-1)) {
    ith_inters &lt;- list()
    for (j in (i+1):N) {
      ith_inters[[ names(all_gene_vectors)[j ]]] &lt;- intersect(all_gene_vectors[[i]], all_gene_vectors[[j]])
    }
    pairwise_inters[[ names(all_gene_vectors)[i] ]] &lt;- ith_inters
  }
  
  #-------------------------------------------------------------------------------------------------------
  #Generate table in wide format:
  #-------------------------------------------------------------------------------------------------------  
  #Concatenate gene names of all tables to a separate file
  #Extract unique values in this file
  #Perform outer join between the separate file and each table
  
  all_names &lt;- c(data.list)
  for (tp in timepoints){
    all_names &lt;- append(all_names, tables[[tp]]$genes)
  }
  all_names &lt;- unique(all_names)
  
  wide_table &lt;- matrix(nrow=length(all_names), ncol=(1+length(timepoints)))
  wide_table &lt;- as.data.frame(wide_table)
  colnames(wide_table) &lt;- append("genes", timepoints)
  
  for (tp in timepoints){
    tmp &lt;- tables[[tp]]
    tmp<span class="math-container">$tp &lt;- 1
    tmp &lt;- merge(wide_table, tmp, by="genes", all=T)
    wide_table$</span>tp &lt;- tmp$tp
  }
  
  
  set.seed(11)
  BCG_validation_Up &lt;- sample(letters[1:429], 20)
  BCG_discovery_Up &lt;- sample(letters[1:250], 20)
  MTB_validation_Up &lt;- sample(letters[1:286], 20)
  MTB_discovery_Up &lt;- sample(letters[1:128], 20)
  BCG_validation_Down &lt;- sample(letters[1:267], 20)
  BCG_discovery_Down  &lt;- sample(letters[1:350], 20)
  MTB_validation_Down &lt;- sample(letters[1:244], 20)
  MTB_discovery_Down &lt;- sample(letters[1:86], 20)
  
  cross_table &lt;- matrix(, nrow = 4, ncol = 4)
  rownames(cross_table) &lt;- c("BCG_validation_Up", "BCG_discovery_Up", "MTB_validation_Up", "MTB_discovery_Up")
  colnames(cross_table) &lt;- c("BCG_validation_Down", "BCG_discovery_Down", "MTB_validation_Up", "MTB_discovery_Up")
  
  for (i in 1:4){
    for(j in 1:4){
      cross_table[i,j] &lt;- length(intersect(get(paste0("",i,"_Up")),(get(paste0("",j,"_Down")))))
    }
  } 
  
  cross_table
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-29T14:25:54.837Z
Comments.Text: Please show how the end table should look like, and give a small example to test with.
Comments.UserDisplayName: benn
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-29T14:43:07.690Z
Comments.Text: It looks like you already have code. Can you describe why what you have is not acceptable?
Comments.UserDisplayName: winni2k
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-29T15:05:39.077Z
Comments.Text: @winni2k Yes but I get the following error code when I attempt to execute it: Error in ith_inters[[names(all_gene_vectors)[j]]] <- intersect(all_gene_vectors[[i]],  : 
    attempt to select less than one element in OneIndex
Comments.UserDisplayName: user3762
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-29T15:08:05.067Z
Comments.Text: @b.nota I have inserted a screenshot of the table I would like to be filled in, please find it above
Comments.UserDisplayName: user3762
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-29T15:27:09.507Z
Comments.Text: That's a confusion matrix of 4 x 4, but you have 8 lists. Do you want a 8 x 8 confusion matrix or do you combine some of the lists?
Comments.UserDisplayName: benn
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-29T15:40:03.950Z
Comments.Text: Please don't post screenshots of code! That just makes the question heavier to load, useless for people using screen readers and, most importantly, makes the code you show useless to us. We need to be able to copy your code and run it locally to test any solutions we come up with. Please [edit] your question and include the code here directly. Use the [formatting tools](https://bioinformatics.stackexchange.com/help/formatting) to format it as code.
Comments.UserDisplayName: terdon
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-29T16:41:50.857Z
Comments.Text: @b.nota What i meant was a 4x4 table with 4 of them referring to upregulated genes and the other 4 for down regulated genes
Comments.UserDisplayName: user3762
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-29T16:55:18.097Z
Comments.Text: @terdon Thanks for letting me know, I didn't know that it was so inconvenient for you and others. Please find my question edited.
Comments.UserDisplayName: user3762
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-29T17:05:44.487Z
Comments.Text: @user3762, that was absolutely not clear from the question!
Comments.UserDisplayName: benn
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-29T17:15:16.990Z
Comments.Text: @b.nota Apologies, I hope it is all clear now
Comments.UserDisplayName: user3762
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-30T00:13:15.480Z
Comments.Text: @b.nota Thanks for your suggested answer. I will check to see it works fine. Just wanted to ask why you have used (,20) in particular in all of the brackets? And why 11 in set.seed?
Comments.UserDisplayName: user3762
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-30T07:52:55.440Z
Comments.Text: I made a workable example by generating lists of random letters (20 letters each). The set.seed command is to get the same letters each time. I made this workable example because you did not give a workable example.
Comments.UserDisplayName: benn
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-30T09:15:09.953Z
Comments.Text: Hi @user3762, I am sensing a bit of confusion and frustration in the comments to your question. I think some of that could be avoided if you edited your post to be about a Minimal, Complete, and Verifiable example as described here: https://stackoverflow.com/help/mcve
Comments.UserDisplayName: winni2k
Tags: <r><rstudio>
Title: Creating a computational table to show the number of available genes between all possible pairs of combinations in eight gene lists
ThreadText: # Creating a computational table to show the number of available genes between all possible pairs of combinations in eight gene lists
  
  (Asked by: username_0 on 2018-11-29)
  
  I am looking to create a table (on RStudio) that will show the overlap between 8 different gene lists wherever possible. I have saved the 8 lists as .txt files all in one folder and set that particular folder as my working directory. Any idea of code that will work for this?
  
  [![Output table with NA values](https://i.sstatic.net/Pn9A1.png)](https://i.sstatic.net/Pn9A1.png)
  
  If anyone knows what I can alter to make the current code work or suggest an alternative way I would be very happy to hear any suggestions!
  
  ```
  data.file1 <- "GSE108363_BCGdown_V.txt"
  data.file2 <- "GSE108363_BCGdown_D.txt"
  data.file3 <- "GSE108363_BCGup_V.txt"
  data.file4 <- "GSE108363_BCGup_D.txt"
  data.file5 <- "GSE108363_MTBdown_V.txt"
  data.file6 <- "GSE108363_MTBdown_D.txt"
  data.file7 <- "GSE108363_MTBup_V.txt"
  data.file8 <- "GSE108363_MTBup_D.txt"
  
  genevect1 <- scan(data.file1, what=character(), sep="\n")
  genevect2 <- scan(data.file2, what=character(), sep="\n")
  genevect3 <- scan(data.file3, what=character(), sep="\n")
  genevect4 <- scan(data.file4, what=character(), sep="\n")
  genevect5 <- scan(data.file5, what=character(), sep="\n")
  genevect6 <- scan(data.file6, what=character(), sep="\n")
  genevect7 <- scan(data.file7, what=character(), sep="\n")
  genevect8 <- scan(data.file8, what=character(), sep="\n")
  
  filelist <- list(data.file1, data.file2, data.file3, data.file4, data.file5, data.file6, data.file7, data.file8)
  #-------------------------------------------------------------------------------------------------------
  # "Lapply" is basically making a "for loop".
  #-------------------------------------------------------------------------------------------------------
  all_gene_vectors <- lapply(filelist, scan, what=character(), sep="\n")
  
  #-------------------------------------------------------------------------------------------------------
  # Then make the intersection of *all* genesets
  #-------------------------------------------------------------------------------------------------------
  final_inter <- all_gene_vectors[[1]]
  for (next_genevect in all_gene_vectors[2:length(all_gene_vectors)]) {
    final_inter <- intersect(final_inter, next_genevect)
  }
  show(final_inter)
  
  #-------------------------------------------------------------------------------------------------------
  # Or compute all pairwise intersections (untested)
  #-------------------------------------------------------------------------------------------------------
  pairwise_inters <- list()
  #-------------------------------------------------------------------------------------------------------
  # Iterate over all possible pairs i,j (i ≠ j)
  #-------------------------------------------------------------------------------------------------------
  N <- length(all_gene_vectors)
  for (i in 1:(N-1)) {
    ith_inters <- list()
    for (j in (i+1):N) {
      ith_inters[[ names(all_gene_vectors)[j ]]] <- intersect(all_gene_vectors[[i]], all_gene_vectors[[j]])
    }
    pairwise_inters[[ names(all_gene_vectors)[i] ]] <- ith_inters
  }
  
  #-------------------------------------------------------------------------------------------------------
  #Generate table in wide format:
  #-------------------------------------------------------------------------------------------------------  
  #Concatenate gene names of all tables to a separate file
  #Extract unique values in this file
  #Perform outer join between the separate file and each table
  
  all_names <- c(data.list)
  for (tp in timepoints){
    all_names <- append(all_names, tables[[tp]]$genes)
  }
  all_names <- unique(all_names)
  
  wide_table <- matrix(nrow=length(all_names), ncol=(1+length(timepoints)))
  wide_table <- as.data.frame(wide_table)
  colnames(wide_table) <- append("genes", timepoints)
  
  for (tp in timepoints){
    tmp <- tables[[tp]]
    tmp$tp <- 1
    tmp <- merge(wide_table, tmp, by="genes", all=T)
    wide_table$tp <- tmp$tp
  }
  
  set.seed(11)
  BCG_validation_Up <- sample(letters[1:429], 20)
  BCG_discovery_Up <- sample(letters[1:250], 20)
  MTB_validation_Up <- sample(letters[1:286], 20)
  MTB_discovery_Up <- sample(letters[1:128], 20)
  BCG_validation_Down <- sample(letters[1:267], 20)
  BCG_discovery_Down  <- sample(letters[1:350], 20)
  MTB_validation_Down <- sample(letters[1:244], 20)
  MTB_discovery_Down <- sample(letters[1:86], 20)
  
  cross_table <- matrix(, nrow = 4, ncol = 4)
  rownames(cross_table) <- c("BCG_validation_Up", "BCG_discovery_Up", "MTB_validation_Up", "MTB_discovery_Up")
  colnames(cross_table) <- c("BCG_validation_Down", "BCG_discovery_Down", "MTB_validation_Up", "MTB_discovery_Up")
  
  for (i in 1:4){
    for(j in 1:4){
      cross_table[i,j] <- length(intersect(get(paste0("",i,"_Up")),(get(paste0("",j,"_Down")))))
    }
  } 
  
  cross_table
  
  ```
  
  **Tags:** r, rstudio
  
  > **Comments:**
  > **username_1**: Please show how the end table should look like, and give a small example to test with.  
  > **username_2**: It looks like you already have code. Can you describe why what you have is not acceptable?  
  > **username_0**: @username_2 Yes but I get the following error code when I attempt to execute it: Error in ith_inters[[names(all_gene_vectors)[j]]] <- intersect(all_gene_vectors[[i]],  :    attempt to select less than one element in OneIndex  
  > **username_0**: @b.nota I have inserted a screenshot of the table I would like to be filled in, please find it above  
  > **username_1**: That's a confusion matrix of 4 x 4, but you have 8 lists. Do you want a 8 x 8 confusion matrix or do you combine some of the lists?  
  > **username_3**: Please don't post screenshots of code! That just makes the question heavier to load, useless for people using screen readers and, most importantly, makes the code you show useless to us. We need to be able to copy your code and run it locally to test any solutions we come up with. Please [edit] your question and include the code here directly. Use the [formatting tools](https://bioinformatics.stackexchange.com/help/formatting) to format it as code.  
  > **username_0**: @b.nota What i meant was a 4x4 table with 4 of them referring to upregulated genes and the other 4 for down regulated genes  
  > **username_0**: @username_3 Thanks for letting me know, I didn't know that it was so inconvenient for you and others. Please find my question edited.  
  > **username_1**: @username_0, that was absolutely not clear from the question!  
  > **username_0**: @b.nota Apologies, I hope it is all clear now  
  > **username_0**: @b.nota Thanks for your suggested answer. I will check to see it works fine. Just wanted to ask why you have used (,20) in particular in all of the brackets? And why 11 in set.seed?  
  > **username_1**: I made a workable example by generating lists of random letters (20 letters each). The set.seed command is to get the same letters each time. I made this workable example because you did not give a workable example.  
  > **username_2**: Hi @username_0, I am sensing a bit of confusion and frustration in the comments to your question. I think some of that could be avoided if you edited your post to be about a Minimal, Complete, and Verifiable example as described here: https://stackoverflow.com/help/mcve  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-11-29. Score: 1
  
  To make a confusion matrix of 4 lists against 4 other lists, you need to get the length of all intersecting genes for each pair. Here an example with letters instead of gene names.
  
  ```
  set.seed(11)
  list1_up <- sample(letters[1:26], 20)
  list1_down <- sample(letters[1:26], 20)
  list2_up <- sample(letters[1:26], 20)
  list2_down <- sample(letters[1:26], 20)
  list3_up <- sample(letters[1:26], 20)
  list3_down <- sample(letters[1:26], 20)
  list4_up <- sample(letters[1:26], 20)
  list4_down <- sample(letters[1:26], 20)
  
  cross_table <- matrix(, nrow = 4, ncol = 4)
  rownames(cross_table) <- c("list1_up", "list2_up", "list3_up", "list4_up")
  colnames(cross_table) <- c("list1_down", "list2_down", "list3_down", "list4_down")
  
  for (i in 1:4){
    for(j in 1:4){
      cross_table[i,j] <- length(intersect(get(paste0("list",i,"_up")),(get(paste0("list",j,"_down")))))
      }
  } 
  
  cross_table
           list1_down list2_down list3_down list4_down
  list1_up         15         14         15         15
  list2_up         18         15         15         15
  list3_up         17         15         14         16
  list4_up         17         15         15         16
  
  ```
  
  > **Comments:**
  > **username_0**: Please could you look at my attempt at altering this code to suit my data and see where I have gone wrong? I have added the code to my question above  
  > **username_1**: No, I will not look at your attempt. Here is my suggestion, do with it what you want.  
  > **username_4**: @username_0 It would be better if you describe what has gone wrong and how the solution here fails you. Also show your attempts to fix or adapt this solution into one that works for you. Good luck!  
  > **username_5**: @username_1 nice manners  
  
  ---
  
  