【问题标题】:Combine csv files with common file identifier将 csv 文件与通用文件标识符相结合
【发布时间】:2017-11-24 20:37:48
【问题描述】:

我有一个大约 500 个 csv 文件的列表,每个文件的文件名由一个六位数字和一个年份组成(例如 123456_2015.csv)。我想将所有具有相同六位数字的文件附加在一起。我试图实现这个问题中建议的代码: Import and rbind multiple csv files with common name in R 但我希望将附加数据保存为与当前保存原始文件相同的目录中的新 csv 文件。我也尝试实现以下代码,但是由此生成的 csv 文件不包含数据。

rm(list=ls())

filenames <- list.files(path = "C:/Users/smithma/Desktop/PM25_test")

NAPS_ID <- gsub('.+?\\([0-9]{5,6}?)\\_.+?$', '\\1', filenames)
Unique_NAPS_ID <- unique(NAPS_ID)
n <- length(Unique_NAPS_ID)

for(j in 1:n){
      curr_NAPS_ID <- as.character(Unique_NAPS_ID[j])
      NAPS_ID_pattern <- paste(".+?\\_(", curr_NAPS_ID,"+?)\\_.+?$", sep = "" )
      NAPS_filenames <- list.files(path = "C:/Users/smithma/Desktop/PM25_test", pattern = NAPS_ID_pattern)
      write.csv(do.call("rbind",  lapply(NAPS_filenames, read.csv, header = TRUE)),file = paste("C:/Users/smithma/Desktop/PM25_test/MERGED", "MERGED_", Unique_NAPS_ID[j], ".csv", sep = ""), row.names=FALSE)
}

任何帮助将不胜感激。

【问题讨论】:

    标签: r dataframe append


    【解决方案1】:

    你可能会考虑这样的事情:

    ##will take the first 6 characters of each file name
    six.digit.filenames <- substr(filenames, 1,6)
    path <- "C:/Users/smithma/Desktop/PM25_test/"
    unique.numbers <- unique(six.digit.filenames)
    
    for(j in unique.numbers){
      sub <- filenames[which(substr(filenames,1,6) == j)]
      data.for.output <- c()
            for(file in sub){
              ##now do your stuff with these files including read them in
              data <- read.csv(paste0(path,file))
              data.for.output <- rbind(data.for.output,data)
            }
      write.csv(data.for.output,paste0(path,j, '.csv'), row.names = F)
    }
    

    【讨论】:

      【解决方案2】:

      因为您没有进行任何数据操作,所以您不需要将文件视为表格数据。您只需要复制文件内容即可。

      filenames <- list.files("C:/Users/smithma/Desktop/PM25_test", full.names = TRUE)
      
      NAPS_ID <- substr(basename(filenames), 1, 6)
      Unique_NAPS_ID <- unique(NAPS_ID)
      
      for(curr_NAPS_ID in Unique_NAPS_ID){
        NAPS_filenames <- filenames[startsWith(basename(filenames), curr_NAPS_ID)]
        output_file <- paste0(
          "C:/Users/nwerth/Desktop/PM25_test/MERGED_", curr_NAPS_ID, ".csv"
        )
        for (fname in NAPS_filenames) {
          line_text <- readLines(fname)
          # Write the header from the first file
          if (fname == NAPS_filenames[1]) {
            cat(line_text[1], '\n', sep = '', file = output_file)
          }
          # Append every line in the file except the header
          line_text <- line_text[-1]
          cat(line_text, file = output_file, sep = '\n', append = TRUE)
        }
      }
      

      我的改变:

      • list.files(..., full.names = TRUE) 通常是最好的选择。
      • 因为数字出现在文件名的开头,我建议substr。浏览代码时更容易了解发生了什么。
      • 不是循环遍历向量的索引,而是遍历值。如果向量为空,它会更简洁且不太可能导致问题。
      • startsWithendsWith 是比较新的功能,而且很棒。
      • 您只关心复制行,所以只需使用readLines 将它们放入并使用cat 将它们取出。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-05
        • 2015-03-15
        • 1970-01-01
        • 2016-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多