The str_like() function in R is “used to detect a pattern in the same way as SQL’s LIKE operator”. It checks whether a character string matches a pattern specified as a string and returns a logical vector of the same length as the input vector, with TRUE suggesting a match and FALSE suggesting no match.
Syntax
str_like(string, pattern, ignore_case = TRUE)
Parameters
- 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.
Example 1
library(stringr)
cars <- c("BMW", "Audi", "Mercedez", "Porche")
str_like(cars, "Mercedez")
Output
[1] FALSE FALSE TRUE FALSE
Example 2
library(stringr)
cars <- c("bmw", "Audi", "Mercedez", "Porche")
pattern <- "b__"
str_like(cars, pattern)
Output
[1] TRUE FALSE FALSE FALSE
Conclusion
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.
That’s it.

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.