【问题标题】:R rename columns in many CSVsR重命名许多CSV中的列
【发布时间】:2020-05-15 11:12:11
【问题描述】:

我有 200 个具有相同两列(日期和级别)的 csv。但是,这些列当前没有名称。对于我在 R 中使用的包,它们都需要具有相同的列名。有没有办法循环遍历所有 CSV 并为它们提供所有列名(相同的列名、日期和级别)?我是 R 新手,没有太多编写循环的经验。

例如,目前每个 CSV 中的数据如下所示:

09/21/1299 | 23
09/22/1999 | 25
09/23/1999 | 25

但我希望它看起来像这样:

date       | level
09/21/1299 | 23
09/22/1999 | 25
09/23/1999 | 25

【问题讨论】:

    标签: r loops csv rename batch-rename


    【解决方案1】:
    1. 使用 list.files(path = "./", pattern = "*.csv)) 获取所有 csv 文件的名称
    2. 创建一个循环,您可以:
      • 读入每个文件
      • 为数据框或矩阵分配列名
      • 写入新文件(或覆盖原始文件)

    【讨论】:

      【解决方案2】:

      在下面的代码中,请更改您想要分配给所有列的wanted_colnames。此代码(如果需要,可进行修复)应读取文件夹 files_folder 内的所有 csv(将其更改为提供完整路径)。最后你可以在dfsrbind_fill/rbindlist 获取完整的数据帧。

      wanted_colnames <- c('var1', 'var2', 'var3')
      files_folder <- '/files/folder/path'
      
      docs <- list.files(files_folder)
      dfs <- list()
      for(doc in docs) {
         full_path <- file.path(files_folder, doc)
         dfs[doc] <- read.csv(full_path)
         names(dfs[doc]) <- wanted_colnames
      }
      

      【讨论】:

        【解决方案3】:

        要访问文件夹中的所有文件,您可以使用list.files,然后您可以使用read.csv 读取每个文件,最后您应该使用write.csv 将它们写入新文件。

        总体而言,您将拥有类似于以下的代码:

        files <- list.files("path/to/directory/")
        sapply(files, function(file){
                              x <- read.csv(file)
                              colnames(x) <- c("Col1", "Col2")
                              write.csv(paste0("new_", file),x)
        }
        

        【讨论】:

          【解决方案4】:

          我假设您不只是想读取文件,而是实际修改文件以使其包含列标题。

          为了使以下代码工作,您需要定义两个变量:path 应该指向存储原始文件的文件夹。 out_path 应该是文件夹的路径,修改后的文件应该存储在其中。如果文件夹out_path 不存在,则会创建它。

          这段代码读取path中的所有csv文件,添加header并将修改后的文件写入文件夹out_path

          # create the output folder
          # showWarnings = FALSE ensures that the function does not complain,
          # even if the folder already exists
          dir.create(out_path, showWarnings = FALSE)
          
          # get the names of the input files with their full path
          files <- list.files(path, "\\.csv", full.name = TRUE)
          
          # loop through all the input files
          for (file in files) {
          
            # read the file, specify the correct separator
            data <- read.table(file, sep = "|")
          
            # set the column names
            names(data) <- c("date", "level")
          
            # define the output file name: the file should be written to
            # out_path and have the same name as the original file
            outfile <- file.path(out_path, basename(file))
          
            # write the file. You need to specify the separator (|), and
            # omit row names and quotes
            write.table(data, outfile, sep = "|", row.names = FALSE, quote = FALSE)
          }
          

          您问题中的示例文件将变成:

          date|level
          09/21/1299 |23
          09/22/1999 |25
          09/23/1999 |25
          

          请注意,标题没有很好地对齐。如果文件被读取为 csv 文件,这应该不是问题。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-10-21
            • 1970-01-01
            • 1970-01-01
            • 2021-12-03
            • 1970-01-01
            • 2016-09-07
            • 2014-05-02
            • 1970-01-01
            相关资源
            最近更新 更多