R -- match,pmatch,charmatch

文章目录

match

R 复制代码
?match
Description
match returns a vector of the positions of (first) matches of its first argument in its second.
第一个向量中的元素在第二个向量中的位置,如果第二个向量中有多个仅返回第一个match 元素的位置,第二个向量中不存在则返回NULL

%in% is a more intuitive interface as a binary operator, which returns a logical vector indicating if there is a match or not for its left operand.

操作符 %in% 是match的简易变形版本,返回结果为TRUE,FALSE

Usage
match(x, table, nomatch = NA_integer_, incomparables = NULL)

x %in% table

Arguments
x	 vector or NULL: the values to be matched. Long vectors are supported.

table	vector or NULL: the values to be matched against. Long vectors are not supported.

nomatch	 the value to be returned in the case when no match is found. Note that it is coerced to integer.
如果没有match,应该返回什么值,默认是NULL

incomparables	 a vector of values that cannot be matched. Any value in x matching a value in this vector is assigned the nomatch value. For historical reasons, FALSE is equivalent to NULL.
类似于排除第一个向量中的某些元素,当第一个向量中的某些元素match 该参数指定的向量时,则返回NULL。

Examples
# 取两个向量的交集
intersect <- function(x, y) y[match(x, y, nomatch = 0)]
intersect(1:10, 7:20)

1:10 %in% c(1,3,5,9)
sstr <- c("c","ab","B","bba","c",NA,"@","bla","a","Ba","%")
sstr[sstr %in% c(letters, LETTERS)]

pmatch

R 复制代码
?pmatch
Description
pmatch seeks matches for the elements of its first argument among those of its second.

Usage
pmatch(x, table, nomatch = NA_integer_, duplicates.ok = FALSE)

Arguments
x	the values to be matched: converted to a character vector by as.character. Long vectors are supported.

table	the values to be matched against: converted to a character vector. Long vectors are not supported.

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

duplicates.ok	should elements be in table be used more than once?
这个参数控制该函数的行为,决定第二个向量中可以match几次,是遇到第一个match的就终止match还是寻找所有可以match的

Examples
pmatch("", "")                             # returns NA
pmatch("m",   c("mean", "median", "mode")) # returns NA    <--- 更具这一个示例,我觉得还是不要考虑使用这个函数了,通常我们会认为应该返回一个位置的,但返回了NA,容易给人造成误导。
pmatch("me",c("mean", "median", "mode"),duplicates.ok = TRUE) # returns NA
pmatch("med", c("mean", "median", "mode")) # returns 2

pmatch(c("", "ab", "ab"), c("abc", "ab"), dup = FALSE)
pmatch(c("", "ab", "ab"), c("abc", "ab"), dup = TRUE)
## compare
charmatch(c("", "ab", "ab"), c("abc", "ab"))

charmatch

R 复制代码
?charmatch
Description
charmatch seeks matches for the elements of its first argument among those of its second.

Usage
charmatch(x, table, nomatch = NA_integer_)

Arguments
x	the values to be matched: converted to a character vector by as.character. Long vectors are supported.

table	the values to be matched against: converted to a character vector. Long vectors are not supported.

nomatch		the (integer) value to be returned at non-matching positions.

if multiple exact or multiple partial matches are found then 0 is returned and if no match is found then nomatch is returned.

Examples
charmatch("", "")                             # returns 1
charmatch("m",   c("mean", "median", "mode")) # returns 0
charmatch("med", c("mean", "median", "mode")) # returns 2
相关推荐
有梦想的Frank博士1 天前
R语言*号标识显著性差异判断组间差异是否具有统计意义
开发语言·信息可视化·r语言
逆风远航4 天前
R语言贝叶斯:INLA下的贝叶斯回归、生存分析、随机游走、广义可加模型、极端数据的贝叶斯分析
开发语言·r语言·贝叶斯·生态学·结构方程·环境科学·混合效应
Faxxtty4 天前
【R语言】解决package ‘qvalue’ is not available (for R version 3.6.1)
开发语言·r语言
Cachel wood5 天前
Vue前端框架:Vue前端项目文件目录
java·前端·vue.js·python·算法·r语言·前端框架
邢博士谈科教6 天前
OmicsTools软件和R语言分析环境安装配置答疑汇总最新版
r语言
纪伊路上盛名在6 天前
vscode中提升效率的插件扩展——待更新
linux·服务器·ide·vscode·python·r语言·编辑器
拓端研究室TRL6 天前
银行信贷风控专题:Python、R 语言机器学习数据挖掘应用实例合集:xgboost、决策树、随机森林、贝叶斯等...
python·决策树·机器学习·数据挖掘·r语言
在在进步6 天前
R数据结构&向量基础
数据结构·r语言
琼火hu8 天前
R语言笔记(五):Apply函数
开发语言·笔记·r语言·apply
扛着仪器去爬山8 天前
R语言结构方程模型(SEM)
r语言·贝叶斯·生态学·结构方程·混合效应·sem