0
votes

I would like to know if there is an easy way to skip characters using the read_fwf from the readr package in R.

For example, modifying one of the examples in the documentation

library(readr)
fwf_sample <- system.file("extdata/fwf-sample.txt", package = "readr")
read_fwf(fwf_sample, fwf_widths(c(2, -3,2, 3)))

throws the error:

Error: Begin offset (2) must be smaller than end offset (-1)

Using the base read.fwf function works just fine however:

read.fwf(fwf_sample, widths = c(2,-3,2,3))

#  V1 V2  V3
#1 12 67 890
#2 12 67 890
#3 12 67 890
#4 12 67 890
#5 12 67 890

Is there a way I can mimic this behaviour using readr::read_fwf? (I am interested mostly for performance reason).

1
No error on my machine.b Using readr version 1.0.0. in R 3.3.1. On the other hand the negation is not honored and I get 4 columns.IRTFM
I was on Ubuntu 64 bit with R 3.3.1 and readr 0.2.2.. I upgraded to 1.0.0 and I get the same issue now.. I think I'll stick to read.fwf for the moment...lucacerone

1 Answers

1
votes

The help page suggests using fwf_positions:

> read_fwf(fwf_sample, fwf_positions(c(1, 5, 8), c(2, 7, 10),  col_names=paste0("V", 1:3)) )
Parsed with column specification:
cols(
  V1 = col_character(),
  V2 = col_character(),
  V3 = col_character()
)
# A tibble: 3 x 3
     V1    V2    V3
  <chr> <chr> <chr>
1    Jo    Sm   ith
2    Ma    Ha   rtf
3    Ev    No   lan