Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

Search strings from dict within a data table column values

$
0
0

There is a data.table dt with a column with text sentences in each row (dt$text). Then, there is a dictionary with words (smaller data.table with phrases column: dict$word and lookup_n column with a number).

I need to go through each column value in dt and if a phrase from dictionary is a part of dt string, put a value from lookup_n column of dict in a dt column "lookup_num". Which is the fastest way to do it? I know, that I can search text in text string with: grepl("Search_word, "Text_to_search", fixed=TRUE). I try to do the following (example):

dt = data.table( text = c('cat, dog books.', 'horse', 'kits fits. mits, bits')) 
dt$yes <- ''
dt$lookup_num <- 0
dt

dict = data.table( word = c('cat, dog ', 'kits'), lookup_n = c(8, 7))

#working!
for(i in 1:nrow(dt)) {
  for (j in 1:nrow(dict)){
    if (dt[i, 'yes'] == ''& grepl(dict[j,word], dt[i,text], fixed=TRUE)) { 
              dt[i,'yes'] <- dict[j,word]
              dt[i,'lookup_num'] <- dict[j,lookup_n]}

  }
}
dt

Also, is there any faster way, than looping through dt and dict?


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>