The str_like() is a function from package stringr in R that tests whether a character string matches a pattern specified as a string. It returns a logical vector of the same length as the input vector, with TRUE suggesting a match and FALSE suggesting no match.
str_like(string, pattern, ignore_case = TRUE)
string: Input vector. Either a character vector or something coercible to one.
pattern: A character vector containing a SQL “like” pattern.
ignore_case: Ignore case of matches? Defaults to TRUE to match the SQL LIKE operator.
library(stringr) cars <- c("BMW", "Audi", "Mercedez", "Porche") str_like(cars, "Mercedez")
 FALSE FALSE TRUE FALSE
In this example, the str_like() function returns a logical vector of length 4, with TRUE suggesting that the corresponding element of cars matches the pattern “Mercedez”.
The stringr package provides many other useful functions for working with strings, such as str_replace(), str_extract(), and str_detect().
library(stringr) cars <- c("bmw", "Audi", "Mercedez", "Porche") pattern <- "b__" str_like(cars, pattern)
In the above code, we used the str_like() function to search for strings in the cars vector that match the pattern “b__”.
In this pattern, the two underscores (__) represent any two characters, so this pattern matches any string in the cars vector that begins with the letter “b” followed by any two characters.
The str_like() function returns a logical vector of length 4, with TRUE suggesting that the first element of cars matches the pattern “b__” and FALSE indicating that the other elements do not match the pattern.
The str_like() function follows the conventions of the SQL LIKE operator.
- It must match the entire string.
- The _ matches a single character (like .).
- The % matches any number of characters (like .*).
- The \% and \_ match literal % and _.
- The match is case-insensitive by default.
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.