当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R语言 char.expand()用法及代码示例


R 语言中的 char.expand() 函数用于在其第二个元素中寻找其第一个参数的唯一匹配。如果成功,则返回该元素;否则,它将执行由第三个参数指定的操作。

用法: char.expand(input, target, nomatch = stop(“no match”))
参数: 
input: character string to be expanded 
target:character vector with the values to be matched against 
nomatch: an R expression to be evaluated in case expansion was not possible 
 

范例1:

Python3


# R program to illustrate
# char.expand function
 
# Creating string vector
x <- c("sand", "and", "land")
 
# Calling char.expand() function
char.expand("a", x, warning("no expand"))
char.expand("an", x, warning("no expand"))
char.expand("and", x, warning("no expand"))

输出:



[1] "and"
[1] "and"
[1] "and"

范例2:

Python3


# R program to illustrate
# char.expand function
 
# Creating string vector
x <- c("sand", "and", "land")
 
# Calling char.expand() function
char.expand("G", x, warning("no expand"))  

输出:

[1] NA
Warning message:
In eval(nomatch):no expand

相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 Seeking a unique match of elements in R Programming – char.expand() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。