<p>Don't you just need a binary matrix with presence/absence data for all of the genes?</p>

<pre><code>library(dplyr)
combined &lt;- full_join(data.frame(gene=ONE, GMP_MONO=1),
                      data.frame(gene=TWO, CMP=1))
combined[is.na(combined)] &lt;- 0
</code></pre>

<p>Edit:
To loop over a large number of data frames, the easiest solution is to use the <code>reduce</code> function from the <a href="https://github.com/tidyverse/purrr" rel="nofollow noreferrer">purrr</a> package:</p>

<pre><code>combined &lt;- reduce(list(data.frame(gene=c('gene1', 'gene2', 'gene3'), set1=1),
                        data.frame(gene=c('gene3', 'gene4', 'gene5'), set2=1),
                        data.frame(gene=c('gene1', 'gene4'), set3=1)
                       ), full_join)
combined[is.na(combined)] &lt;- 0
combined
#    gene set1 set2 set3
# 1 gene1    1    0    1
# 2 gene2    1    0    0
# 3 gene3    1    1    0
# 4 gene4    0    1    1
# 5 gene5    0    1    0
</code></pre>

CC BY-SA 4.0
2018-05-31T05:19:35.910Z
Im getting NA in the CMP column in my combined data-frame after doing full_join , im getting 0 as well but not sure why NA..
kcm
CC BY-SA 4.0
2018-05-31T05:51:33.867Z
Oh yeah. I forgot about that. See updated answer
heathobrien
CC BY-SA 4.0
2018-05-31T06:40:28.837Z
yeah now its working but i do see error when i try to do more than ,which is apparently wont happen with full_join ,lets say i do want to intersect , more than three data frame and make a binary matrix can you suggest what more to add to your code?
kcm
CC BY-SA 4.0
2018-05-31T07:33:16.910Z
see updated answer
heathobrien
CC BY-SA 4.0
2018-05-30T14:56:48.537Z
2018-05-31T07:32:42.327Z
2018-05-31T07:32:42.327Z
heathobrien
<p>We can plot using lists as well, using <code>fromList</code> function, see example:</p>

<pre><code>library(UpSetR)

myGeneSets &lt;- list(
  set1 = c("gene1","gene2","gene3"),
  set2 = c("gene1","gene4","gene5", "gene7"),
  set3 = c("gene1","gene7")
  )

upset(fromList(myGeneSets))
</code></pre>

<blockquote>
  <p><strong>fromList</strong></p>
  
  <p>A function to convert a list of named vectors to a data frame
  compatible with UpSetR.</p>
</blockquote>

<pre><code>fromList(myGeneSets)
#   set1 set2 set3
# 1    1    1    1
# 2    1    0    0
# 3    1    0    0
# 4    0    1    0
# 5    0    1    0
# 6    0    1    1
</code></pre>

CC BY-SA 4.0
2018-06-04T17:05:16.310Z
another way to do the stuff cool ..i will use this as well
kcm
CC BY-SA 4.0
2018-06-04T10:56:54.540Z
2018-06-04T10:56:54.540Z
zx8754
<p>You need to set a common number of rows, one for each gene (make sure that the gene identifiers are the same for both files). Then in the first column you can set 1 for the file A, and 0 otherwise. In the second column you can do the same.
Then you add this dataframe as the input of the <code>upset</code> function.</p>

<pre><code>genes &lt;- unique(c(ONE, TWO))
df &lt;- data.frame(A = ifelse(genes %in% A, 1, 0), B = ifelse(genes %in% b, 1, 0))
upset(df, ...)
</code></pre>

CC BY-SA 4.0
2018-05-31T06:56:08.510Z
Your solution does work but it looks like what ever intersection it does it gives only one common element ..im not sure what is wrong..
kcm
CC BY-SA 4.0
2018-05-31T07:02:42.597Z
Why would you expect more than one element? Could you share the data of ONE and TWO? Otherwise could you do `length(intersect(ONE, TWO))`?
llrs
CC BY-SA 4.0
2018-05-31T07:14:58.483Z
as I have multiple comparison between lineage type , the ONE ,TWO and now which i added later THREE they are just vector containing my gene list ,so im using only gene list ..
kcm
CC BY-SA 4.0
2018-05-31T07:22:59.933Z
Then you need to add a third column and genes should be made with `genes <- unique(c(ONE, TWO, THREE))`, could you post your data
llrs
CC BY-SA 4.0
2018-05-31T10:02:46.200Z
I will post the data ,update the question with my data.
kcm
CC BY-SA 4.0
2018-05-30T14:57:43.713Z
2018-05-30T14:57:43.713Z
llrs
<p>As i have multiple comparison so I m trying this <code>UpSet</code> plot, but it needs data to be formatted in particular way, one of the ways is as a list, so now the issue is since between one comparison the no of genes coming out may not be same as the other one, so there is a difference in dimension so if i bind them as data frame then i get dimension error, so the best way to get through is make list so what i have tried so far </p>

<pre><code>A &lt;- read.csv("GMP_MONO_UP_DOWN_CDS_EPI.txt",header = TRUE)
B &lt;- read.csv("CMP_GMP_UP_DOWN_EPI.txt",header = TRUE)
C &lt;- read.csv("HSC_BLAST_UPDOWN_EPI.txt",header = TRUE,sep = '\t')

dim(A)
dim(B)
ONE &lt;- A[c(1)]#############just taking out the gene column########
TWO &lt;- B[c(1)]#############just taking out the gene column########
THREE &lt;- B[c(1)]#############just taking out the gene column########
dim(ONE)
dim(TWO)
b1 &lt;- apply(ONE,2,list) 
b2 &lt;- apply(TWO,2,list)
</code></pre>

<p>Now this is the code from their help page </p>

<pre><code>upset(movies, nsets = 7, nintersects = 30, mb.ratio = c(0.5, 0.5),
      order.by = c("freq", "degree"), decreasing = c(TRUE,FALSE))
</code></pre>

<p>so the data "movies" class is data frame, there is not dimension issue as such as in my case if i want to intersect two comparison which may not have the same length, so how do i overcome the issue any suggestion or help would be highly appreciated. Update my question with one more data to add.</p>

<r><upsetr>
Preparing data for UpSet Plot in R
# Preparing data for UpSet Plot in R

(Asked by: username_0 on 2018-05-30)

As i have multiple comparison so I m trying this UpSet plot, but it needs data to be formatted in particular way, one of the ways is as a list, so now the issue is since between one comparison the no of genes coming out may not be same as the other one, so there is a difference in dimension so if i bind them as data frame then i get dimension error, so the best way to get through is make list so what i have tried so far 
A <- read.csv("GMP_MONO_UP_DOWN_CDS_EPI.txt",header = TRUE)
B <- read.csv("CMP_GMP_UP_DOWN_EPI.txt",header = TRUE)
C <- read.csv("HSC_BLAST_UPDOWN_EPI.txt",header = TRUE,sep = '\t')

dim(A)
dim(B)
ONE <- A[c(1)]#############just taking out the gene column########
TWO <- B[c(1)]#############just taking out the gene column########
THREE <- B[c(1)]#############just taking out the gene column########
dim(ONE)
dim(TWO)
b1 <- apply(ONE,2,list) 
b2 <- apply(TWO,2,list)

Now this is the code from their help page 
upset(movies, nsets = 7, nintersects = 30, mb.ratio = c(0.5, 0.5),
      order.by = c("freq", "degree"), decreasing = c(TRUE,FALSE))

so the data "movies" class is data frame, there is not dimension issue as such as in my case if i want to intersect two comparison which may not have the same length, so how do i overcome the issue any suggestion or help would be highly appreciated. Update my question with one more data to add.

**Tags:** r, upsetr

---

## 3 answers

### Answer by username_1 on 2018-05-30. Score: 3 | Accepted answer

Don't you just need a binary matrix with presence/absence data for all of the genes?
library(dplyr)
combined <- full_join(data.frame(gene=ONE, GMP_MONO=1),
                      data.frame(gene=TWO, CMP=1))
combined[is.na(combined)] <- 0

Edit:
To loop over a large number of data frames, the easiest solution is to use the reduce function from the purrr package:
combined <- reduce(list(data.frame(gene=c('gene1', 'gene2', 'gene3'), set1=1),
                        data.frame(gene=c('gene3', 'gene4', 'gene5'), set2=1),
                        data.frame(gene=c('gene1', 'gene4'), set3=1)
                       ), full_join)
combined[is.na(combined)] <- 0
combined
#    gene set1 set2 set3
# 1 gene1    1    0    1
# 2 gene2    1    0    0
# 3 gene3    1    1    0
# 4 gene4    0    1    1
# 5 gene5    0    1    0

> **Comments:**
> **username_0**: Im getting NA in the CMP column in my combined data-frame after doing full_join , im getting 0 as well but not sure why NA..  
> **username_1**: Oh yeah. I forgot about that. See updated answer  
> **username_0**: yeah now its working but i do see error when i try to do more than ,which is apparently wont happen with full_join ,lets say i do want to intersect , more than three data frame and make a binary matrix can you suggest what more to add to your code?  
> **username_1**: see updated answer  

---

### Answer by username_2 on 2018-06-04. Score: 7

We can plot using lists as well, using fromList function, see example:
library(UpSetR)

myGeneSets <- list(
  set1 = c("gene1","gene2","gene3"),
  set2 = c("gene1","gene4","gene5", "gene7"),
  set3 = c("gene1","gene7")
  )

upset(fromList(myGeneSets))


fromList
A function to convert a list of named vectors to a data frame
  compatible with UpSetR.

fromList(myGeneSets)
#   set1 set2 set3
# 1    1    1    1
# 2    1    0    0
# 3    1    0    0
# 4    0    1    0
# 5    0    1    0
# 6    0    1    1

> **Comments:**
> **username_0**: another way to do the stuff cool ..i will use this as well  

---

### Answer by username_3 on 2018-05-30. Score: 1

You need to set a common number of rows, one for each gene (make sure that the gene identifiers are the same for both files). Then in the first column you can set 1 for the file A, and 0 otherwise. In the second column you can do the same.
Then you add this dataframe as the input of the upset function.
genes <- unique(c(ONE, TWO))
df <- data.frame(A = ifelse(genes %in% A, 1, 0), B = ifelse(genes %in% b, 1, 0))
upset(df, ...)

> **Comments:**
> **username_0**: Your solution does work but it looks like what ever intersection it does it gives only one common element ..im not sure what is wrong..  
> **username_3**: Why would you expect more than one element? Could you share the data of ONE and TWO? Otherwise could you do `length(intersect(ONE, TWO))`?  
> **username_0**: as I have multiple comparison between lineage type , the ONE ,TWO and now which i added later THREE they are just vector containing my gene list ,so im using only gene list ..  
> **username_3**: Then you need to add a third column and genes should be made with `genes <- unique(c(ONE, TWO, THREE))`, could you post your data  
> **username_0**: I will post the data ,update the question with my data.  

---

