This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Getting intersect between three tables of genes in Rstudio

Hi,

I have 3 tables of gene list from 3 type of tissues with 2 columns. The first column is ID and the second column is gene. I want to find the intersect between these 3 tables of genes. Does anyone can help me on how to get the intersect using Rstudio? Really need some help.

Thank you.

r rstudio intersect

Use something like this which returns all elements of table1, column 1, that are also in table 2, column1. Taking genes to be in column1.

t1<-data.frame(gene=c("G1","G2","G3","G4"),value=c(1,2,3,4))
t2<-data.frame(gene=c("G1","G2","G3","G5"),value=c(1,2,3,5))
newTable <- t1[is.element(t1[,1],t2[,1]),]

Thank you for the answer. But i have 3 tables. How to find the intersect between 3 tables?

Hi, here is a reproducible example for you to play with. FWIW my answer was based on learning about elements and selecting from data.frame/matrix structures and is probably not the best way, as seen in other answers.

t1<-data.frame(gene=c("G1","G2","G3","G4"),value=c(1,2,3,4)
t2<-data.frame(gene=c("G1","G2","G3","G5"),value=c(1,2,3,5))
t3<-data.frame(gene=c("G2","G3","G5","G6"),value=c(2,3,5,6))
newTable <- t1[is.element(t1[is.element(t1[,1],t2[,1]),1],t3[,1]),]

You can see that G2, G3 are the only genes in each table, and this is evident from newTable object. Think about what each component is doing, and how the elements of the table (as vectors) are being accessed and compared.

Thank you sir for the suggesstion. But, the problem is, my table is very big. I have 100,000 plus rows for every table. Is there any way more suited with my situation sir?

1 answer

This Q has nothing to do with Rstudio. This is a very basic task in R and learning it will pay you off very well in the long run.

Hint: intersect(intersect(table1$ID, table2$ID), table3$ID)

I am sorry sir. I am a newbie. I have found like below:

v1 <- c("geneA","geneB",""...)

v2 <- c("geneA","geneC",""...)

v3 <- c("geneD","geneE",""...)

v4 <- c("geneA","geneE",""...)

v5 <- c("geneB","geneC",""...)

Reduce(intersect,list(a,b,c,d,e))

So, I try with my data like below:

v1 <- c(bark)

v2 <- c(leaf)

v3 <- c(latex)

Reduce (intersect, list(v1, v2, v3))

list()

But, I just got "list()". What does it mean?where is the intersect data?

Not trying to be cheeky but if you ignore his far more simple (and correct) suggestion...why would he answer again?

For your information mr. Kennth. I am not ignoring his suggestions but i found the same answer like him which mention that >intersect(intersect(table1....)) is same like >reduce(intersect,list(table1...)). So, i am doing the same thing. Please don't bother if you don't have anything to help here.

Sorry to bother you, but the real problem here is you just copying examples from anywhere without understanding the underlying concept. This will lead you nowhere!

Mr. Santosh, please don't simply say that i just copying. I am totally understand what i am doing. I already tried your suggestion to use

Hint: intersect(intersect(table1$ID, table2$ID), table3$ID)

But, after running that script, i just got this

intersect(intersect(v1,v2),v3) list()

The only problem is just i can't see the result. I just want to know where i can see the intersection table. The output is just "list()" but where is actually the list?

Log in to answer this question.