字符串匹配与R字词典
String Matching with word dictionary with R
我有一个单词 table (wt) 像这样 (3 x 3 )
ungrateful mango uncertain
hobby prejudicial meat
persecution bird honest
和字典 (dict)
persecution
overpowering
prejudicial
offense
ungrateful
uncertain
musical
murderous
detest
youth
我想用 dict 搜索 wt 中的所有单词,如果有任何单词与字典匹配,则会给出字典中单词位置table,不匹配的单词将被自动删除。
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)
}
}
但是当我期待这样的输出时,这是错误的
[,1] [,2] [,3]
[1,] 5 6
[2,] 3
[3,] 1
我是 R 的新手,对语法不太了解。请帮忙
match
函数returns第一个参数在第二个参数中的匹配位置。 (如果有多个匹配项,则只返回第一个匹配项的位置。)然后我们将其转换为对应于 wt
矩阵位置的矩阵。
matrix(match(wt, dict), nrow=nrow(wt))
[,1] [,2] [,3]
[1,] 5 NA 6
[2,] NA 3 NA
[3,] 1 NA NA
和上面@epi10一样,charmatch
matrix(charmatch(wt,dict), nrow = nrow (wt))
和匹配
matrix(pmatch(wt,dict), nrow = nrow (wt))
同样有效。
我有一个单词 table (wt) 像这样 (3 x 3 )
ungrateful mango uncertain
hobby prejudicial meat
persecution bird honest
和字典 (dict)
persecution
overpowering
prejudicial
offense
ungrateful
uncertain
musical
murderous
detest
youth
我想用 dict 搜索 wt 中的所有单词,如果有任何单词与字典匹配,则会给出字典中单词位置table,不匹配的单词将被自动删除。
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)
}
}
但是当我期待这样的输出时,这是错误的
[,1] [,2] [,3]
[1,] 5 6
[2,] 3
[3,] 1
我是 R 的新手,对语法不太了解。请帮忙
match
函数returns第一个参数在第二个参数中的匹配位置。 (如果有多个匹配项,则只返回第一个匹配项的位置。)然后我们将其转换为对应于 wt
矩阵位置的矩阵。
matrix(match(wt, dict), nrow=nrow(wt))
[,1] [,2] [,3] [1,] 5 NA 6 [2,] NA 3 NA [3,] 1 NA NA
和上面@epi10一样,charmatch
matrix(charmatch(wt,dict), nrow = nrow (wt))
和匹配
matrix(pmatch(wt,dict), nrow = nrow (wt))
同样有效。