【问题标题】:How to parse chunk of CSV data with white space line breaks into list of data frames如何将带有空白换行符的 CSV 数据块解析为数据帧列表
【发布时间】:2021-12-27 06:44:20
【问题描述】:

我有以下文本文件,它包含几块表。 每个块由空格分隔。

GENERALIZED BORN:
Complex Energy Terms
Frame #,BOND,ANGLE
0,6603.0521,7264
1,7434.9885,7602

Receptor Energy Terms
Frame #,BOND,ANGLE
0,6140.6338,5383.1241
1,6885.2965,5653.6637

Ligand Energy Terms
Frame #,BOND,ANGLE
0,462.4183,1881.428
1,549.692,1949.0482

如何使用 R 将此单个文本文件解析为三个数据框或小标题的列表?

我试过了,但失败了:

library(readr)
readr::read_lines_chunked("myfile.txt", skip =1, chunk_size = 4)

因为readr::read_lines_chunked 无法识别块之间的空白分隔符。

【问题讨论】:

    标签: r tidyverse readr


    【解决方案1】:

    我不清楚该功能是否应该像那样使用。我的猜测不是。但是您可以手动解析数据。创建一个列表,解析出块并将其保存到一个列表中。

    xy <- readLines(con = "test.txt")
    xy <- xy[-1]  # remove GENERALIZED BORN
    
    xy <- xy[which(xy != "")]
    # Start of a break is needed for names and subsetting in a loop.
    breaks <- which(grepl("^.*Energy Terms", x = xy))
    
    dfs <- vector(mode = "list", length = length(breaks))
    names(dfs) <- xy[breaks]
    
    # Adding one accounts for the Energy Terms line. It's either here
    # or in the loop.
    chunks <- breaks + 1
    
    for (chunk in seq_along(chunks)) {
      # If we extract the name and use it to subset, the order of the
      # dfs doesn't really matter.
      chk.name <- xy[chunks[chunk] - 1]
      
      from <- chunks[chunk]
      to <- chunks[chunk + 1] - 2
      
      # When working with the last chunk, this sets the end of the text.
      if (is.na(to)) {
        to <- length(xy)
      }
      
      chk <- xy[from:to]
    
      tmp <- read.table(
        text =  paste(chk, collapse = "\n"), 
        header = TRUE, 
        comment.char = "", 
        sep = ","
        )
      
      dfs[[chk.name]] <- tmp
    }
    

    结果

    $`Complex Energy Terms`
      Frame..     BOND ANGLE
    1       0 6603.052  7264
    2       1 7434.989  7602
    
    $`Receptor Energy Terms`
      Frame..     BOND    ANGLE
    1       0 6140.634 5383.124
    2       1 6885.297 5653.664
    
    $`Ligand Energy Terms`
      Frame..     BOND    ANGLE
    1       0 462.4183 1881.428
    2       1 549.6920 1949.048
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-12
      • 2022-01-18
      • 2011-03-17
      • 2023-01-21
      • 2021-05-10
      • 2019-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多