繁体   English   中英

R 更快的替代 str_contains

[英]R faster alternative to str_contains

我有一个表格和一个字符向量:

ACT_Suburbs_Names <- data.table(DivisionNm = c('ACTON', 'AINSLIE', 'AMAROO', 'ARANDA', 'BANKS'))

temp1 <- c('U1 336 DOORING ST ACTON', '65/78 ACHELON DR MOORONG')

下面的脚本检查 temp1 中的地址是否在 ACT_Suburbs_Names 的地址列表中。

future_sapply(temp1, function(x) str_contains(ACT_Suburbs_Names, x, ignore.case = TRUE)) 

处理我拥有的所有数据需要大量时间。 有更快的吗? 即使在 python 中也可以。

创建一个模式并使用正则表达式:

> library(tidyverse)
> 
> ACT_Suburbs_Names <-  c('ACTON', 'AINSLIE', 'AMAROO', 'ARANDA', 'BANKS')
> 
> pat <- paste(ACT_Suburbs_Names, collapse = '|')
> 
> temp1 <- c('U1 336 DOORING ST ACTON', 
+  '65/78 ACHELON DR MOORONG',
+  'asdf ARANDA asdfsadf',
+           ' asdfasfd   BANK asdf',
+           ' sdafasdf  BANKS  asdf',
+           ' just junk') 
> 
> # find out which entries match - return the index of a match
> 
> grep(pat, temp1)
[1] 1 3 5
>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM