<p>Change dataframe to load all the files into a list:</p>

<p><code>wd &lt;- "C:/all_your_dataframes_in_one_folder"</code></p>

<p><code>setwd(wd)</code></p>

<p>Then you get the list of files you want from your folder</p>

<p><code>files &lt;- list.files()</code></p>

<p>Put into a list with a loop</p>

<pre><code>dflist &lt;- list()
for(i in 1:length(files)){
 dflist[[i]] &lt;- read.csv(files[i])
}
</code></pre>

<p>Then use tidyverse solution </p>

<p><code>library(tidyverse)</code></p>

<pre><code>map(dflist, ~.$GeneID) %&gt;% #creates a list just of the column of interest
    reduce(intersect)  #applies the intersect function cumulatively to the list
</code></pre>

CC BY-SA 4.0
2019-07-31T15:59:34.927Z
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
llrs
CC BY-SA 4.0
2019-07-31T17:25:25.443Z
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..
h3ab74
CC BY-SA 4.0
2019-07-31T17:57:40.357Z
" 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.
kcm
CC BY-SA 4.0
2019-07-31T18:05:58.633Z
@h3ab74  i ran your code    map(dflist, ~.$GeneID) %>% reduce(intersect) 
I get this NULL .Am i doing something wrong
kcm
CC BY-SA 4.0
2019-07-31T18:15:06.160Z
the part of the code with ~.$GeneID is supposed to be the column name of all your dataframes in the list. So, if the column with the gene names in your dataframes does not have the name 'GeneID', change the GeneID part in the code to one that works with your dataframes
h3ab74
CC BY-SA 4.0
2019-07-31T18:37:27.723Z
In my case the column name of all my data frame header is Gene, so i ran this 
    map(dflist, ~.$Gene) %>% 
   reduce(intersect)
character(0)  I get character(0)
kcm
CC BY-SA 4.0
2019-07-31T18:47:53.697Z
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
h3ab74
CC BY-SA 4.0
2019-08-01T04:52:43.007Z
@h3ab74 these are my files https://drive.google.com/open?id=1laieKec1sr4rWa4SIhUvHcLSerrWf4OC my data frame which i import is single column with a header name Gene
kcm
CC BY-SA 4.0
2019-08-17T09:33:07.013Z
it worked ,it had name issues.
kcm
CC BY-SA 4.0
2019-07-31T15:57:49.860Z
2019-07-31T21:14:50.477Z
2019-07-31T21:14:50.477Z
h3ab74
<p>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 .</p>

<p>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 .</p>

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

a &lt;- intersect_all(NewList,NewList1,NewList2,NewList3,NewList4,NewList5,NewList6,NewList7,NewList8)


b &lt;- do.call(rbind.data.frame, a)
</code></pre>

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

<p>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.</p>

<pre><code>filenames &lt;- gsub("\\.csv<span class="math-container">$","", list.files(pattern="\\.csv$</span>"))
for(i in filenames){
  assign(i, read.csv(paste(i, ".csv", sep="")))
}
</code></pre>

<p>The object filenames is <code>character</code> here .It just returns me list of files.</p>

<p>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 .</p>

CC BY-SA 4.0
2019-07-31T07:48:28.740Z
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)
llrs
CC BY-SA 4.0
2019-07-31T08:59:32.873Z
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.
kcm
CC BY-SA 4.0
2019-07-31T09:03:40.263Z
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?
llrs
CC BY-SA 4.0
2019-07-31T09:49:43.930Z
it would be easier if i add my files which im using  https://drive.google.com/open?id=1laieKec1sr4rWa4SIhUvHcLSerrWf4OC
kcm
CC BY-SA 4.0
2019-07-31T09:52:09.907Z
Easier for you but not for me. I don't understand what have to do this files with your problem
llrs
CC BY-SA 4.0
2019-07-31T10:32:50.053Z
okay may be its too confusing but can you let me know if i can how can i read all the csv files in a loop and make them into data-frame that would be easier for me to do the rest
kcm
CC BY-SA 4.0
2019-07-31T10:36:05.170Z
This question is out of the scope of this site. You can find your solution at StackOverflow, specifically [this question](https://stackoverflow.com/questions/11433432/how-to-import-multiple-csv-files-at-once)
llrs
CC BY-SA 4.0
2019-07-31T10:40:49.277Z
sure this helps
kcm
<r>
Intersecting multiple data frames
# Intersecting multiple data frames

(Asked by: username_0 on 2019-07-31)

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 .

**Tags:** r
> **Comments:**
> **username_1**: 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)  
> **username_0**: 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.  
> **username_1**: 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?  
> **username_0**: it would be easier if i add my files which im using  https://drive.google.com/open?id=1laieKec1sr4rWa4SIhUvHcLSerrWf4OC  
> **username_1**: Easier for you but not for me. I don't understand what have to do this files with your problem  
> **username_0**: okay may be its too confusing but can you let me know if i can how can i read all the csv files in a loop and make them into data-frame that would be easier for me to do the rest  
> **username_1**: This question is out of the scope of this site. You can find your solution at StackOverflow, specifically [this question](https://stackoverflow.com/questions/11433432/how-to-import-multiple-csv-files-at-once)  
> **username_0**: sure this helps  

---

## 1 answers

### Answer by username_2 on 2019-07-31. Score: 1 | Accepted answer

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

> **Comments:**
> **username_1**: 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  
> **username_2**: 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..  
> **username_0**: " 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.  
> **username_0**: @h3ab74  i ran your code    map(dflist, ~.$GeneID) %>% reduce(intersect)  I get this NULL .Am i doing something wrong  
> **username_2**: the part of the code with ~.$GeneID is supposed to be the column name of all your dataframes in the list. So, if the column with the gene names in your dataframes does not have the name 'GeneID', change the GeneID part in the code to one that works with your dataframes  
> **username_0**: In my case the column name of all my data frame header is Gene, so i ran this      map(dflist, ~.$Gene) %>%     reduce(intersect) character(0)  I get character(0)  
> **username_2**: 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  
> **username_0**: @h3ab74 these are my files https://drive.google.com/open?id=1laieKec1sr4rWa4SIhUvHcLSerrWf4OC my data frame which i import is single column with a header name Gene  
> **username_0**: it worked ,it had name issues.  

---

