Properties of pmatch function

百般思念 提交于 2021-01-27 14:34:00

问题


I don't understand the behavior of the built-in function pmatch (partial string matching).

The description provides the following example:

pmatch("m",   c("mean", "median", "mode")) # returns NA instead of 1,2,3

but using:

pmatch("m", "mean") # returns 1, as I would have expected. 

Could anybody explain to me this behavior?


回答1:


As per the documentation:

nomatch: the value to be returned at non-matching or multiply partially matching positions. Note that it is coerced to integer.

The nomatch defaults to NA (i.e., if there are multiple partial matches then NA will be returned).

pmatch("me",   c("mean", "median", "mode")) 
[1] NA  # returns NA instead of 1,2 since multiple partial matches

pmatch("mo",   c("mean", "median", "mode")) 
[1] 3   # since single partial match



回答2:


Use grep instead - the NA-on-duplicates behavior of pmatch is incredibly annoying:

grep("^m",   c("mean", "median", "mode"))
[1] 1 2 3

> grep("ed",   c("mean", "median", "mode"))
[1] 2

The only downside is that pmatch(x, table... is vectorized for both args, but grep only for the second arg. So grep can't take a vector of patterns. But you can use stringi, or else sapply.



来源:https://stackoverflow.com/questions/52219724/properties-of-pmatch-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!