0
$\begingroup$

I have to intersect multiple data frame to find common elements. For visualization purpose i can use upset Plot but it doesn;t return a object containing the intersection object .I tried jvenn but at max it can take six sets to intersect .

Im able to do the interesction but i would like to know if i can do it more efficiently ,which apparently not sure how to do this is my code as of now where i read each file as data frame, covert them to list and pass them to do intersection .

NewList <- split(GO0000122, f = seq(nrow(GO0000122)))
NewList1 <- split(GO0006351,f=seq(nrow(GO0006351)))
NewList2 <- split(GO0006355,f=seq(nrow(GO0006355)))
NewList3 <- split(GO0006357,f=seq(nrow(GO0006357)))
NewList4 <- split(GO0006366,f=seq(nrow(GO0006366)))
NewList5 <- split(GO0030154,f=seq(nrow(GO0030154)))
NewList6 <- split(GO0045892,f=seq(nrow(GO0045892)))
NewList8 <- split(GO0045893,f=seq(nrow(GO0045893)))
NewList7 <- split(GO0045944,f=seq(nrow(GO0045944)))

a <- intersect_all(NewList,NewList1,NewList2,NewList3,NewList4,NewList5,NewList6,NewList7,NewList8)


b <- do.call(rbind.data.frame, a)

Can i read the files and pass them into list and do the same that would help me from doing lot of things manually

I can read the files from the directory where i have my files but not sure how to pass them to make it as list.

filenames <- gsub("\\.csv$","", list.files(pattern="\\.csv$"))
for(i in filenames){
  assign(i, read.csv(paste(i, ".csv", sep="")))
}

The object filenames is character here .It just returns me list of files.

So how to make my question bit more clear , i would like to read the list of file from the directory and then pass these to perform intersection. Any suggestion or help would be really appreciated .

$\endgroup$
  • $\begingroup$ I've been cleaning some of your post for punctuation, but this one I am not sure how to change it. Could you please edit and correct the comas and i to I and other punctuation errors? Also, if you could clarify what do you mean by intersection here I think that your problem might be easier to solve if you explain what is your goal with this (there might be an out of the box solution) $\endgroup$ – llrs Jul 31 '19 at 7:48
  • $\begingroup$ oh sorry typing partly from phone adds to the problem. My goal is to get set of common elements from multiple data frame where their is only single column consist of genes.So as of now i read each file,then pass those as list, then i do intersect and then turn it into dataframe. So now each time i have to read files if i have 12 files i have to read 12 times .Is there a way to put it in a loop and do the same what i have done above. $\endgroup$ – krushnach Chandra Jul 31 '19 at 8:59
  • $\begingroup$ I think it would be it easier to work with character vectors than lists (it will at least make it easier to see them). But what have to do the files with the intersection, if any first you read and then you intersect, not the other way around or I am missing something here? $\endgroup$ – llrs Jul 31 '19 at 9:03
  • $\begingroup$ it would be easier if i add my files which im using drive.google.com/open?id=1laieKec1sr4rWa4SIhUvHcLSerrWf4OC $\endgroup$ – krushnach Chandra Jul 31 '19 at 9:49
  • 1
    $\begingroup$ This question is out of the scope of this site. You can find your solution at StackOverflow, specifically this question $\endgroup$ – llrs Jul 31 '19 at 10:36
1
$\begingroup$

Change dataframe to load all the files into a list:

wd <- "C:/all_your_dataframes_in_one_folder"

setwd(wd)

Then you get the list of files you want from your folder

files <- list.files()

Put into a list with a loop

dflist <- list()
for(i in 1:length(files)){
 dflist[[i]] <- read.csv(files[i])
}

Then use tidyverse solution

library(tidyverse)

map(dflist, ~.$GeneID) %>% #creates a list just of the column of interest
    reduce(intersect)  #applies the intersect function cumulatively to the list
$\endgroup$
  • $\begingroup$ If you need a package it would be better if you state which ones. Also with readr package you can read already a bunch of csv files in one single call $\endgroup$ – llrs Jul 31 '19 at 15:59
  • 1
    $\begingroup$ I mentioned tidyverse, but I just added library(tidyverse) in the example to be extra clear. Also, I felt as though a loop example would be better for explaining how to construct them.. $\endgroup$ – h3ab74 Jul 31 '19 at 17:25
  • $\begingroup$ " a loop example would be better for explaining how to construct them" this is what im looking for even though i dont use much loop etc because Im sure or my concepts are pretty unclear about it. $\endgroup$ – krushnach Chandra Jul 31 '19 at 17:57
  • $\begingroup$ @h3ab74 i ran your code map(dflist, ~.$GeneID) %>% reduce(intersect) I get this NULL .Am i doing something wrong $\endgroup$ – krushnach Chandra Jul 31 '19 at 18:05
  • 1
    $\begingroup$ Can you edit your question to include an example of how the dataframes are structured so I can see why you're getting nothing? Also, please note that this solution will get you the genes that are similar in all of the dataframes $\endgroup$ – h3ab74 Jul 31 '19 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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