4

I have a word table (wt) like this (3 by 3 )

ungrateful    mango       uncertain
hobby       prejudicial   meat
persecution   bird        honest

and a word dictionary (dict)

persecution
overpowering
prejudicial
offense
ungrateful
uncertain
musical
murderous
detest
youth

I want to search all words in the wt with the dict and if any word match with the dictionary, that will give the dictionary word position in the word table, and the words which do not match will be automatically deleted.

    wt <- matrix(c("ungrateful","mango", "uncertain","hobby", "prejudicial", "meat","persecution","bird","honest"), nrow = 3, ncol = 3, byrow = TRUE)
    dict<- matrix(c(
"persecution",
"overpowering",
"prejudicial",
"offense",
"ungrateful",
"uncertain",
"musical",
"murderous",
"detest",
"youth"), nrow = 10, ncol = 1, byrow = FALSE)

for (i in 1:nrow(df)){
        for (i in 1:col(df)){
                x[i,j ] <- charmatch(df[i,j],dict_word)
        }          
}

But this is giving error, when I am expecting output like this

     [,1] [,2] [,3]
 [1,]  5         6
 [2,]      3
 [3,]  1

I am pretty new in R and don't have good idea about the syntax . please help.

eipi10
  • 91,525
  • 24
  • 209
  • 285
bim
  • 612
  • 7
  • 18

2 Answers2

4

The match function returns the position of matches of its first argument in its second. (If there's more than one match, only the position of the first match is returned.) Then we convert that to a matrix corresponding to the positions of the wt matrix.

matrix(match(wt, dict), nrow=nrow(wt))
     [,1] [,2] [,3]
[1,]    5   NA    6
[2,]   NA    3   NA
[3,]    1   NA   NA
eipi10
  • 91,525
  • 24
  • 209
  • 285
  • OMG, that was this easy! Thanks a lot really. I was trying so many things. – bim Feb 17 '16 at 17:49
  • @bipu You can click the check mark on the left of the answer to "accept" it, indicating that it solved your problem. – Frank Feb 17 '16 at 18:01
  • @eipi10, I was looking for some suggestions on how we can get the data for some specific equivalent column of the match from 2nd dataset. I have posted the question at - http://stackoverflow.com/questions/42749447/r-fuzzy-string-match-to-return-specific-column-based-on-matched-string It would be great help if you can suggest something – user1412 Mar 12 '17 at 17:20
2

In the same way as @epi10 mentioned above, charmatch

matrix(charmatch(wt,dict), nrow = nrow (wt))

and pmatch

matrix(pmatch(wt,dict), nrow = nrow (wt))

works as well.

bim
  • 612
  • 7
  • 18