字符串与带R的单词词典匹配
我有一个这样的单词表(wt)(3乘3) 和一本单词词典(dict) 我想用dict搜索wt中的所有单词,如果任何单词与词典匹配,这将给出词典单词在单词表中的位置,不匹配的单词将自动删除字符串与带R的单词词典匹配,r,string,pattern-matching,string-matching,R,String,Pattern Matching,String Matching,我有一个这样的单词表(wt)(3乘3) 和一本单词词典(dict) 我想用dict搜索wt中的所有单词,如果任何单词与词典匹配,这将给出词典单词在单词表中的位置,不匹配的单词将自动删除 wt <- matrix(c("ungrateful","mango", "uncertain","hobby", "prejudicial", "meat","persecution","bird","honest"), nrow = 3, ncol = 3, byrow = TRUE)
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)
}
}
我是R方面的新手,对语法不太了解。请提供帮助。函数
match
返回其第一个参数在第二个参数中的匹配位置。(如果有多个匹配,则只返回第一个匹配的位置。)然后我们将其转换为与wt
矩阵位置对应的矩阵
matrix(match(wt, dict), nrow=nrow(wt))
与上面提到的@epi10相同,charmatch
matrix(charmatch(wt,dict), nrow = nrow (wt))
matrix(pmatch(wt,dict), nrow = nrow (wt))
和pmatch
matrix(charmatch(wt,dict), nrow = nrow (wt))
matrix(pmatch(wt,dict), nrow = nrow (wt))
同样有效 天哪,这太容易了!真的非常感谢。我尝试了很多东西。@bipu您可以单击答案左侧的复选标记“接受”它,这表明它解决了您的问题。@eipi10,我正在寻找一些建议,说明如何从第二个数据集获取匹配的特定等效列的数据。我已将问题发布在-如果你能提出一些建议,那将非常有帮助
matrix(charmatch(wt,dict), nrow = nrow (wt))
matrix(pmatch(wt,dict), nrow = nrow (wt))