2

I have a vector of character strings (v1) like so:

> head(v1)
[1] "do_i_need_to_even_say_it_do_i_well_here_i_go_anyways_chris_cornell_in_chicago_tonight"                                       
[2] "going_to_see_harry_sunday_happiness"                                                                                         
[3] "this_motha_fucka_stay_solid_foh_with_your_naieve_ass_mentality_your_synapsis_are_lacking_read_a_fucking_book_for_christ_sake"
[4] "why_twitter_will_soon_become_obsolete_http_www.imediaconnection.com_content_23465_asp"                                       
[5] "like_i_said_my_back_still_fucking_hurts_and_im_going_to_complain_about_it_like_no_ones_business_http_tumblr.com_x6n25amd5"   
[6] "my_picture_with_kris_karmada_is_gone_forever_its_not_in_my_comments_on_my_mysapce_or_on_my_http_tumblr.com_xzg1wy4jj"

And another vector of character strings (v2) like so:

> head(v2)
[1] "here_i_go" "going" "naieve_ass" "your_synapsis"   "my_picture_with"   "roll" 

What is the quickest way that I can return a list of vectors where each list item represents each vector item in v1 and each vector item is a regular expression match where an item in v2 appeared in that v1 item, like so:

[[1]]
[1] "here_i_go"

[[2]]
[1] "going"

[[3]]
[1] "naieve_ass"      "your_synapsis"

[[4]]

[[5]]
[1] "going"

[[6]]
[1] "my_picture_with"
jazzurro
  • 23,179
  • 35
  • 66
  • 76
Christopher Costello
  • 1,186
  • 2
  • 16
  • 30
  • 1
    You can get a nice logical matrix with `sapply(v2, grepl, v1)` or a list with `do.call(Map, c(function(...){v2[c(...)]}, lapply(v2, function(x){grepl(x, v1)})))` – alistaire Feb 17 '18 at 03:29

2 Answers2

2

If you want speed, I'd use stringi. You don't seem to have any regex, just fixed patterns, so we can use a fixed stri_extract, and (since you don't mention what to do with multiple matches) I'll assume only extracting the first match is fine, giving us a little more speed with stri_extract_first_fixed.

It's probably not worth benchmarking on such a small example, but this should be quite fast.

library(stringi)
matches = lapply(v1, stri_extract_first_fixed, v2)
lapply(matches, function(x) x[!is.na(x)])
# [[1]]
# [1] "here_i_go"
# 
# [[2]]
# [1] "going"
# 
# [[3]]
# [1] "naieve_ass"    "your_synapsis"
# 
# [[4]]
# character(0)
# 
# [[5]]
# [1] "going"

Thanks for sharing data, but next time please share it copy/pasteably. dput is nice for that. Here's a copy/pasteable input:

v1 = c(
"do_i_need_to_even_say_it_do_i_well_here_i_go_anyways_chris_cornell_in_chicago_tonight"                                       ,
"going_to_see_harry_sunday_happiness"                                                                                         ,
"this_motha_fucka_stay_solid_foh_with_your_naieve_ass_mentality_your_synapsis_are_lacking_read_a_fucking_book_for_christ_sake",
"why_twitter_will_soon_become_obsolete_http_www.imediaconnection.com_content_23465_asp"                                       ,
"like_i_said_my_back_still_fucking_hurts_and_im_going_to_complain_about_it_like_no_ones_business_http_tumblr.com_x6n25amd5"  , 
"my_picture_with_kris_karmada_is_gone_forever_its_not_in_my_comments_on_my_mysapce_or_on_my_http_tumblr.com_xzg1wy4jj")

v2 = c("here_i_go", "going", "naieve_ass", "your_synapsis",   "my_picture_with",   "roll" )
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
2

I'd like to leave another option with stri_extract_all_regex() in the stringi package. You can create your regular expression directly from v2 and use it in pattern.

library(stringi)

stri_extract_all_regex(str = v1, pattern = paste(v2, collapse = "|"))

[[1]]
[1] "here_i_go"

[[2]]
[1] "going"

[[3]]
[1] "naieve_ass"    "your_synapsis"

[[4]]
[1] NA

[[5]]
[1] "going"

[[6]]
[1] "my_picture_with"
jazzurro
  • 23,179
  • 35
  • 66
  • 76