简体   繁体   English

字符串与R字词典匹配

[英]String Matching with word dictionary with R

I have a word table ( wt ) like this (3 by 3 ) 我有一个像这样的单词表( wt )(3乘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. 我想用dict搜索wt中的所有单词,如果有任何单词与字典匹配,那么将在单词表中给出字典单词的位置,并且将自动删除不匹配的单词。

    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 . 我是R的新手,对语法不太了解。 please help. 请帮忙。

The match function returns the position of matches of its first argument in its second. match函数返回其第二个参数的匹配位置。 (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. (如果存在多个匹配,则仅返回第一个匹配的位置。)然后,我们将其转换为与wt矩阵的位置对应的矩阵。

matrix(match(wt, dict), nrow=nrow(wt))
  [,1] [,2] [,3] [1,] 5 NA 6 [2,] NA 3 NA [3,] 1 NA NA 

In the same way as @epi10 mentioned above, charmatch 与上面提到的@ epi10一样, charmatch

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

and pmatch pmatch

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

works as well. 也有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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