Skip to content

read_fwf confused by ragged lines #469

@MatthieuStigler

Description

@MatthieuStigler

In a slightly weird data, where missing values are simply indicated by terminating the line earlier, there seem to be a confusion with fwf_empty(tmp) and wrong output, while if given manually, the output is right but shows many parsing warnings?

library(readr)
library(foreign)
txt <- c("    7.98      6.74          7.75            8.00", "    2.03      2.38", 
     "    5.82      5.09", "    21.6      15.1          14.5            14.5")
tmp <- tempfile()
writeLines(txt, tmp)
cat(read_file(tmp))

7.98      6.74          7.75            8.00
2.03      2.38
5.82      5.09
21.6      15.1          14.5            14.5

It is ok with read.fwf:
wid <- c(8, 10, 14, 16)
read.fwf(tmp, wid) # works

     V1    V2    V3   V4
1  7.98  6.74  7.75  8.0
2  2.03  2.38    NA   NA
3  5.82  5.09    NA   NA
4 21.60 15.10 14.50 14.5

But several issues with read_fwf:

  • fwf_empty(): wrong output

    read_fwf(tmp, fwf_empty(tmp)) ## wrong output?
     X1    X2    X3                                         X4
      <dbl> <dbl> <dbl>                                      <chr>
    1  7.98  6.74  7.75                                       8.00
    2  2.03  2.38    NA .6      15.1          14.5            14.5
    
  • fwf_widths(): right output but parsing warnings?

      read_table(tmp) ## similar issue
    read_fwf(tmp, fwf_widths(wid)) # right output but parsing warnings?
    Warning: 4 parsing failures.
    row col  expected    actual
      2  X3 14 chars  0        
      2  -- 4 columns 3 columns
      3  X3 14 chars  0        
      3  -- 4 columns 3 columns
    
      <dbl> <dbl> <dbl> <dbl>
    1  7.98  6.74  7.75   8.0
    2  2.03  2.38    NA    NA
    3  5.82  5.09    NA    NA
    4 21.60 15.10 14.50  14.5
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions