like.Rd
The like operator is a simple wrapper for grep(..., value=TRUE)
, whose complexity is hard to crack for R-newbies.
x %like% pattern
x %like any% pattern
Follows the logic of simple SQL or basic commands.
a vector (numeric, character, factor), matching the mode of x
match
, pmatch
, grep
, %[]%
, %overlaps%
# find names ending on "or"
names(d.pizza) %like% "%or"
#> [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
#> [13] FALSE FALSE FALSE FALSE
# find names starting with "d"
names(d.pizza) %like% "d%"
#> [1] FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE
#> [13] FALSE FALSE FALSE FALSE
# ... containing er?
names(d.pizza) %like% "%er%"
#> [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE
#> [13] TRUE TRUE FALSE FALSE
# and combined, search for a name containing "un", ending on "or"
# or beginning with "F"
levels(d.pizza$driver) %like any% c("%un%", "%or", "F%")
#> [1] FALSE FALSE FALSE TRUE TRUE FALSE TRUE
# the positions on the vector
match(names(d.pizza) %like% "%er%", names(d.pizza))
#> [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA