The charmatch() function in R is “used to find to seek matches for the elements of its first argument among those of its second“. For example, charmatch(c(“a”, “b”, “c”), c(“a”, “c”)) returns c(1, NA, 2).
charmatch(x, table, nomatch = NA_integer_)
- x: It is the values to be matched.
- table: It is the values to be matched against.
- nomatch: It is the (integer) value to be returned at non-matching positions.
The charmatch() function returns an integer vector of the same length as the input R object, giving the indices of the items in the table which matched or nomatch.
Example 1: R program to use charmatch() Function
Using the c() function with three elements, let’s create a vector.
var <- c("data", "meta", "sata") charmatch("me", var)
You can see that the charmatch() function returns the index of the matching element.
Example 2: Partial match
Exact matches are preferred to partial matches. Let’s pass on the different arguments.
var <- c("data", "meta", "sata") charmatch("sa", var)
If there is a single exact match or no exact match and a unique partial match, then the index of the matching value is returned; if multiple exact or multiple partial matches are found, then 0 is returned, and if no match is found, then nomatch is returned.
If no match is found, then it returns NA.
var <- c("data", "meta", "sata") charmatch("ne", var)
Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.