當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。