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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# char.expand functiom
  
# 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"))

chevron_right


Output :

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

[1] NA
Warning message:
In eval(nomatch) : no expand
My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.