简体   繁体   中英

R faster alternative to str_contains

I've got a table and a character vector:

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

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

The script below checks that the addresses in temp1 are in the list of addresses from ACT_Suburbs_Names.

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

It takes an enormous amount of time to process all the data that I've got. Anything faster? even in python would be ok.

Create a pattern and use regular expressions:

> 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
>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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