Merging multiple csv files with name sequence

﹥>﹥吖頭↗ 提交于 2019-12-24 11:09:37

问题


I am trying to merge 700+ csv files in R. I was able to merge them successfully using the code:

library(dplyr)
library(readr)
df <- list.files(full.names = TRUE) %>% 
  lapply(read_csv) %>% 
  bind_rows 

Now my problem is the file names are saved as flux.0, flux.1, flux.2......flux.733. The R binds the files in order of flux.0, flux.1, flux.10, flux.100, flux.101...and so on. Since the sequence of the file is important for me, can you suggest to incorporate this in the above code? Many Thanks for the help!


回答1:


My pipeline for things like that is to get the list of all the files (as you did), turn it into a tbl/data.frame and than using mapto read the files and unnest() them. That why I can keep the path/file name for each file I loaded.

require(tidyverse)

df <- list.files(path = "path",
                       full.names = TRUE,
                       recursive = TRUE,
                       pattern = "*.csv") %>% 
tbl_df() %>%
mutate(data = map(value, read.csv)) %>%
arrange(value) %>%
unnest(data) 



回答2:


Here you have another answer using your own approach. I've just added a function that reads the csv and adds a new column called 'file' with the name of the file without the extension.

library(dplyr) 
library(readr) 

df <- list.files(full.names = TRUE) %>% 
    lapply(function(x) {a <- read_csv(x);
                        mutate(a, file = tools::file_path_sans_ext(basename(x)))}) %>%
    bind_rows


来源:https://stackoverflow.com/questions/54782183/merging-multiple-csv-files-with-name-sequence

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