lookup location of character string in a dictionary vector

Viewed 64

I have data in the following format.

dictionary<-c("a", "the", "we", "is", "hello")
text<-vector(mode="list",length=2)
text[[1]]<-c("we","hello","relative")
text[[2]]<-c("because","is")

[[1]]
[1] "we"       "hello"    "relative"

[[2]]
[1] "because" "is"      "the"    

I would like to lookup the location of each word in my text in the dictionary and create a tokenized text list that would look like this:

token.text<-vector(mode="list",length=2)
token.text[[1]]<-c(3,5,0)
token.text[[2]]<-c(0,4)

[[1]]
[1] 3 5 0

[[2]]
[1] 0 4

My data is much bigger of course. My dictionary is relatively small (2000 words) but my text list is large. I'm sure there's a simple solution, but I'm at a loss here.

1 Answers

We could use match

lapply(text, match, table = dictionary, nomatch = 0)
#[[1]]
#[1] 3 5 0

#[[2]]
#[1] 0 4
Related