[英]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.