R readr :: read_fwf使用fwf_widths忽略字符 [英] R readr::read_fwf ignore characters using fwf_widths
本文介绍了R readr :: read_fwf使用fwf_widths忽略字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有一种简单的方法可以使用R中的readr包中的read_fwf来跳过字符.
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)))
引发错误:
Error: Begin offset (2) must be smaller than end offset (-1)
使用基本的read.fwf函数可以很好地工作:
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
是否可以使用readr::read_fwf
模仿此行为?
(我主要是出于性能方面的考虑).
Is there a way I can mimic this behaviour using readr::read_fwf
?
(I am interested mostly for performance reason).
推荐答案
帮助页面建议使用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
这篇关于R readr :: read_fwf使用fwf_widths忽略字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文