pmatch 以错误的方式返回字符匹配

pmatch returning character match in wrong way

我有一个字符词矩阵(3 x 3)和一个字典(n x 1)。我想找到与字典匹配的字符,将替换为字典中存在的字符位置,并删除所有不匹配的单词(NA 或 NULL)。我试过简单的 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)

'b'中的nrow不正确。应该是5

b <- matrix(c("456_rr","522_kk","456_ss", "432_tt", 
     "432_uu"), nrow = 5, ncol = 1) 
v1 <- pmatch(a,b)
m1 
#[1]  1 NA  3  5  4 NA  2 NA NA

dim(m1) <- dim(a)
m1
#     [,1] [,2] [,3]
#[1,]    1    5    2
#[2,]   NA    4   NA
#[3,]    3   NA   NA

如果我们需要获取重复项的索引,请使用默认为 FALSE

duplicates.ok=TRUE
`dim<-`(pmatch(a, b, duplicates.ok=TRUE), dim(a))
#     [,1] [,2] [,3]
#[1,]    1    5    2
#[2,]    1    4    2
#[3,]    3   NA    2