Merging of multiple excel files in R

て烟熏妆下的殇ゞ 提交于 2021-02-19 02:33:29

问题


I am getting a basic problem in R. I have to merge 72 excel files with similar data type having same variables. I have to merge them to a single data set in R. I have used the below code for merging but this seems NOT practical for so many files. Can anyone help me please?

data1<-read.csv("D:/Customer_details1/data-01.csv")

data2<-read.csv("D:/Customer_details2/data-02.csv")

data3<-read.csv("D:/Customer_details3/data-03.csv")

data_merged<-merge(data1,data2,all.x = TRUE, all.y = TRUE)

data_merged2<-merge(data_merged,data3,all.x = TRUE, all.y = TRUE)

回答1:


First, if the extensions are .csv, they're not Excel files, they're .csv files.

We can leverage the apply family of functions to do this efficiently.

First, let's create a list of the files:

setwd("D://Customer_details1/")

#  create a list of all files in the working directory with the .csv extension
files <- list.files(pattern="*.csv")

Let's use purrr::map in this case, although we could also use lapply - updated to map_dfr to remove the need for reduce, by automatically rbind-ing into a data frame:

require(purrr)

mainDF <- files %>% map_dfr(read.csv) 

You can pass additional arguments to read.csv if you need to: map(read.csv, ...)

Note that for rbind to work the column names have to be the same, and I'm assuming they are based on your question.



来源:https://stackoverflow.com/questions/46305724/merging-of-multiple-excel-files-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!