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
相关推荐
jiang_changsheng8 小时前
环境管理工具全景图与深度对比
java·c语言·开发语言·c++·python·r语言
JicasdC123asd10 小时前
使用Faster R-CNN模型训练汽车品牌与型号检测数据集 改进C4结构 优化汽车识别系统 多类别检测 VOC格式
r语言·cnn·汽车
请你喝好果汁64116 小时前
## 学习笔记:R 语言中比例字符串的数值转换,如GeneRatio中5/100的处理
笔记·学习·r语言
怦怦蓝16 小时前
DB2深度解析:从架构原理到与R语言的集成实践
开发语言·架构·r语言·db2
新新学长搞科研17 小时前
【CCF主办 | 高认可度会议】第六届人工智能、大数据与算法国际学术会议(CAIBDA 2026)
大数据·开发语言·网络·人工智能·算法·r语言·中国计算机学会
Piar1231sdafa1 天前
战斗车辆状态识别与分类 --- 基于Mask R-CNN和RegNet的模型实现
r语言·cnn
陳土2 天前
R语言Offier包源码—1:read_docx()
r语言
善木科研喵2 天前
IF5.9分,α-硫辛酸如何缓解化疗神经毒性?网络毒理学结合网络药理学双重锁定关键通路!
数据库·数据分析·r语言·sci·生信分析·医学科研
Piar1231sdafa3 天前
椅子目标检测新突破:Cascade R-CNN模型详解与性能优化_1
目标检测·r语言·cnn
Loacnasfhia93 天前
基于Mask R-CNN与RegNetX的钢水罐及未定义物体目标检测系统研究_1
目标检测·r语言·cnn