[R] merging multiple .csv files

Spencer Graves @pencer@gr@ve@ @end|ng |rom e||ect|vede|en@e@org
Tue Dec 15 23:40:48 CET 2020


	  Did you work the examples in help("merge")?  Also, have you looked at 
the "dplyr" package?  It has 9 different vignettes.  The lead author is 
Hadley Wickham, who won the 2019 COPSS Presidents' Award for work like 
this.


	  Alternatively, you could manually read all 10 files, then figure out 
from that what the merged object should look like with what rows and 
what columns.


	  I might start with something like:


PosAll <- unique(c(table1$Pos, table2$Pos, ... ))


Combined <- data.frame(Pos=PosAll, col1=NA, col2=NA, ...)


	  Then create the object you want that contains a column "Pos" = 
PosAll.  Then:


rownames(table1) <- table1$Pos
rownames(table2) <- table2$Pos
...

sel1 <- PosAll %in% table1$Pos
...

Combined[sel1, names(table1)] <- table1[PosAll[sel1], ]


	  CAVEAT:  I haven't tried this exact code.  It may not work as 
written.  However, I've done things like this in the past.  "merge" and 
"dplyr" have seemed too much like black magic for me.


	  Spencer


On 2020-12-15 16:13, pooja sinha wrote:
> I know that but I do not want to merge them sequentially because I may lose
> some rows which are present in one file while the other doesn't have. I
> googled and found something called multmerge but the code is not working
> for me. I used the following:
> 
> path <-"P:/Documents/Puja Desktop items/Documents/RESULTS/est meth
> results/Final_est_meth_data_rn6/WGBS_exon_sort_CHG/merge_csv"
> 
> 
> filenames <- list.files(path = path)
> 
> View(filenames)
> multmerge = function(path){
>    filenames=list.files(path=path, full.names=TRUE)
>    datalist = lapply(filenames, function(x){read.csv(file=x,header=T)})
>    Reduce(function(x,y) {merge(x,y)}, datalist)
> }
> 
> full_data = multmerge("~/P:/Documents/Puja Desktop
> items/Documents/RESULTS/est meth
> results/Final_est_meth_data_rn6/WGBS_exon_sort_CHG/merge_csv")
> 
> But after running the full_data, the folder is empty.
> 
> Thanks,
> Puja
> 
> On Tue, Dec 15, 2020 at 4:51 PM Bert Gunter <bgunter.4567 using gmail.com> wrote:
> 
>> ?read.csv to read your csv files in data frames
>> ?merge to merge them (sequentially).
>>
>>
>> Bert Gunter
>>
>> "The trouble with having an open mind is that people keep coming along and
>> sticking things into it."
>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>>
>>
>> On Tue, Dec 15, 2020 at 1:36 PM pooja sinha <pjsinha07 using gmail.com> wrote:
>>
>>> Hi All,
>>>
>>> I have 10 .csv files containing 12 to 15 columns but have some columns in
>>> common. I need to join all of my .csv files into one using one common
>>> column ‘Pos’. The header portion of my .csv files looks as shown below:
>>>
>>> Chrom Pos Avg Stdev  A15_3509.C A31_3799.C A32_3800.C A35_3804.C Gene ID
>>> Class ExNum
>>>
>>>   Can anyone help me in getting the code for the above problem. Any help
>>> will be highly appreciated.
>>>
>>>
>>> Thanks,
>>>
>>> Puja
>>>
>>>          [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list