【问题标题】:readr::read_tsv() parsing failures due to trailing tabs由于尾随制表符导致的 readr::read_tsv() 解析失败
【发布时间】:2020-07-13 06:41:40
【问题描述】:

问题/问题

我有 51 列的制表符分隔的 my-data.txt 文件。 col_names 行没有尾随制表符,readr::read_tsv() 正确检测到 51 列。但是,数据列都包含尾随制表符,readr::read_tsv() 将这些错误地解释为具有 52 列。当代码运行时,我收到一个警告,我想摆脱它。是否有任何read_tsv() 参数可以帮助处理这个问题?我应该改用不同的readr 函数吗?

我的数据.txt

PT  AU  BA  CA  GP  RI  OI  BE  Z2  TI  X1  Y1  Z1  FT  PN  AE  Z3  SO  S1  SE  BS  VL  IS  SI  MA  BP  EP  AR  DI  D2  SU  PD  PY  AB  X4  Y4  Z4  AK  CT  CY  SP  CL  TC  Z8  ZB  ZS  Z9  SN  BN  UT  PM
J   Jacquelin, Sebastien; Straube, Jasmin; Cooper, Leanne; Vu, Therese; Song, Axia; Bywater, Megan; Baxter, Eva; Heidecker, Matthew; Wackrow, Brad; Porter, Amy; Ling, Victoria; Green, Joanne; Austin, Rebecca; Kazakoff, Stephen; Waddell, Nicola; Hesson, Luke B.; Pimanda, John E.; Stegelmann, Frank; Bullinger, Lars; Doehner, Konstanze; Rampal, Raajit K.; Heckl, Dirk; Hill, Geoffrey R.; Lane, Steven W.                              Jak2V617F and Dnmt3a loss cooperate to induce myelofibrosis through activated enhancer-driven inflammation                              BLOOD               132 26          2707    2721        10.1182/blood-2018-04-846220            DEC 27 2018 2018                                        10                          WOS:000454429300003     
J   Renne, Julius; Gutberlet, Marcel; Voskrebenzev, Andreas; Kern, Agilo; Kaireit, Till; Hinrichs, Jan; Zardo, Patrick; Warnecke, Gregor; Krueger, Marcus; Braubach, Peter; Jonigk, Danny; Haverich, Axel; Wacker, Frank; Vogel-Claussen, Jens; Zinne, Norman                               Multiparametric MRI for organ quality assessment in a porcine Ex-Vivo lung perfusion system                             PLOS ONE                13  12                  e0209103    10.1371/journal.pone.0209103            DEC 27 2018 2018                                        1                           WOS:000454418200015     
J   Lau, Skadi; Eicke, Dorothee; Oliveira, Marco Carvalho; Wiegmann, Bettina; Schrimpf, Claudia; Haverich, Axel; Blasczyk, Rainer; Wilhelmi, Mathias; Figueiredo, Constanca; Boeer, Ulrike                              Low Immunogenic Endothelial Cells Maintain Morphological and Functional Properties Required for Vascular Tissue Engineering                             TISSUE ENGINEERING PART A               24  5-6         432 447     10.1089/ten.tea.2016.0541           MAR 2018    2018                                        4                           WOS:000418327100001     

代表

请注意,我对 reprex 进行了一些手动编辑,因为我需要读取 .txt 文件以重现问题,但这会导致 reprex 出现错误,而没有我的计算机特定路径)。见RStudio Community Topic 8773

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(readr)

my_data <- read_tsv("my-data.txt", quote = "")
#> Parsed with column specification:
#> cols(
#>   .default = col_logical(),
#>   PT = col_character(),
#>   AU = col_character(),
#>   TI = col_character(),
#>   SO = col_character(),
#>   VL = col_double(),
#>   IS = col_character(),
#>   BP = col_double(),
#>   EP = col_double(),
#>   AR = col_character(),
#>   DI = col_character(),
#>   PD = col_character(),
#>   PY = col_double(),
#>   TC = col_double(),
#>   UT = col_character()
#> )
#> See spec(...) for full column specifications.
#> Warning: 3 parsing failures.
#> row col   expected     actual                                   file
#>   1  -- 51 columns 52 columns 'my-data.txt'
#>   2  -- 51 columns 52 columns 'my-data.txt'
#>   3  -- 51 columns 52 columns 'my-data.txt'

problems(my_data)
#> # A tibble: 3 x 5
#>     row col   expected   actual     file                                  
#>   <int> <chr> <chr>      <chr>      <chr>                                 
#> 1     1 <NA>  51 columns 52 columns 'my-data.txt'
#> 2     2 <NA>  51 columns 52 columns 'my-data.txt'
#> 3     3 <NA>  51 columns 52 columns 'my-data.txt'

reprex package (v0.3.0) 于 2020-04-01 创建

感谢您抽出宝贵时间帮助我。

【问题讨论】:

    标签: r csv parsing readr


    【解决方案1】:

    我最喜欢的 .tsv 文件阅读器是来自 data.table 的fread。它通常开箱即用。可能值得一试。

    library(data.table)
    my_data <- fread("my-data.txt")
    

    【讨论】:

    • 感谢您的建议,伊恩。我试了一下(见下面的reprex)。不幸的是,data.table::fread 通过添加一个额外的列来解释附加选项卡,该列与列名中的数据不对齐,例如,“DOI”不再位于“DI”列中,而是向左移动到“AR”列.感谢您对处理尾随标签的任何其他想法!
    • my_data &lt;- data.table::fread("my-data.txt", quote = "") #&gt; Warning in data.table::fread("/Users/maia/Repositories/responsible-metrics/my-data.txt", : Detected 51 column names but the data has 52 columns (i.e. invalid file). Added 1 extra default column name for the first column which is guessed to be row names or an index. Use setnames() afterwards if this guess is not correct, or fix the file write command that created the file to create a valid file. reprex package (v0.3.0) 于 2020 年 5 月 22 日创建
    猜你喜欢
    • 2022-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多