我想阅读一个可能很大的文本文件,并根据正则表达式动态过滤相关行。我的第一个方法是使用包LaF
,它支持按块读取,然后grepl
过滤。但是,这似乎不起作用:
library(LaF)
fh <- laf_open_csv("myfile.txt", column_types="string", sep="°")
# would be nice to declare *no* separator
fh[grepl("abc", fh[[1]]), ]
在as.character.default(x)
中返回错误 -- 没有将此 S4 转换为字符的方法。似乎grepl
应用于 S4 函数而不是块。
有没有一种很好的方法可以从大文件中读取文本行并有选择地过滤它们?
好的,我刚刚发现了process_blocks
:
regfilter <- function(df, result) c(result, df[grepl("1745", df[[1]]),1])
process_blocks(fh, regfilter)
这行得通,现在我只需要找到一种忽略分隔符的方法。.