Open In App

Seeking a unique match of elements in R Programming – char.expand() Function

Last Updated : 19 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

char.expand() function in R Language is used to seek a unique match of its first argument among the elements of its second. If successful, it returns this element; otherwise, it performs an action specified by the third argument.
 

Syntax: char.expand(input, target, nomatch = stop(“no match”))
Parameters: 
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 
 

Example 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"))


Output : 
 

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

Example 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"))  


Output: 
 

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

 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads