R readr::read_fwf ignore characters using fwf_widths

淺唱寂寞╮ 提交于 2019-12-12 01:44:17

问题


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:


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


来源:https://stackoverflow.com/questions/38962983/r-readrread-fwf-ignore-characters-using-fwf-widths

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!