【问题标题】:How to change Column Names of multiple csv file in R? [duplicate]如何在 R 中更改多个 csv 文件的列名? [复制]
【发布时间】:2020-03-21 20:05:52
【问题描述】:

我有一个包含五个 CSV 文件的数据,如下所示。它有第一行“类别:所有类别”,第二行是列名名称和标记。

Category: All categories    

Name Marks
Mohit 100
Raman 71
Kaveri 45
William 42
Ram Pravesh 37

我想删除数据的第一行,以便所有五个文件一起看起来像这样。

Student Score
Mohit 100
Raman 71
Kaveri 45
William 42
Ram Pravesh 37

我是手动做的,我相信这个问题可能有一些简短的代码。

【问题讨论】:

    标签: r


    【解决方案1】:

    您可以直接在read.csv中指定列名和跳过多少行。

    例如:

    read.csv(file = "yourfile.csv",
             skip = 3, # How many lines you want to skip when you read it
             header = FALSE, # Skip the header too
             col.names = c("Student", "Score"), # Supply your own column names
             stringsAsFactors = FALSE
             )
    

    对于一个完整的可重现示例:

    # Generate data in text format
    raw_file <-
      '
      Category: All categories    
    
    Name, Marks
    Mohit, 100
    Raman, 71
    Kaveri, 45
    William, 42
    Ram Pravesh, 37
    '
    
    # make a temp file to place data
    temp_file <- tempfile(fileext = ".csv")
    
    # write the temp file
    writeLines(raw_file,con = temp_file)
    
    read.csv(file = temp_file,
             skip = 4, # How many lines you want to skip when you read it
             header = FALSE, # Skip the header too
             col.names = c("Student", "Score"), # Supply your own column names
             stringsAsFactors = FALSE
    )
    
    

    这将产生以下结果:

          Student Score
    1       Mohit   100
    2       Raman    71
    3      Kaveri    45
    4     William    42
    5 Ram Pravesh    37
    

    您还提到了读取多个文件:

    # Get all the files that end in a csv in a given folder that you specify
    files_to_read <- list.files(path = "some_path", pattern = ".csv", full.names = T)
    
    # I like `purrr` here because it makes a few things easier
    # Read in and row bind all csv to return a single data frame
    library(purrr)
    out <- map_dfr(files_to_read, ~read.csv(file = .x,
             skip = 4, # How many lines you want to skip when you read it
             header = FALSE, # Skip the header too
             col.names = c("Student", "Score"), # Supply your own column names
             stringsAsFactors = FALSE
    ))
    

    【讨论】:

    • 如何将这些文件保存为单独的文件....以上将所有文件合并到一个文件中..我希望文件保存为多个文件。
    • 而不是map_dfr,只需使用map。这将返回一个列表。如果你想把它们保存出来,那么 map(out, write.csv) @djM
    猜你喜欢
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    • 2021-12-03
    • 2015-01-03
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    相关资源
    最近更新 更多