我有一个字符词矩阵(3 x 3)和一个字典(n x 1)。我想找到与字典匹配的字符,将替换为字典中存在的字符位置,并且将删除所有不匹配的单词(NA 或 NULL)。我试过 simple pmatch
,但它不起作用。我正在寻找这样的结果,
1 5 2
1 4 NA
3 NA 2
但是以这种方式得到结果,[1] 1 NA 3 5 4 NA 2 NA NA
我尝试了下面的代码,
a <- matrix(c("456_rr", "432_uu", "522_kk", "456_rr", "432_tt", "522", "456_ss", "432", "522_kk"), nrow = 3, ncol = 3, byrow = TRUE) # WordTable
b <- matrix(c("456_rr","522_kk","456_ss", "432_tt", "432_uu"), nrow = 5, ncol = 1) # Dictionary
c <- pmatch(a, b)
print(c)