Post

Pattern matching in r (grep and grepl)

Pattern matching in r (grep and grepl)

grep and grepl commands are used for pattern matching in r.

1
2
grep_result <- grep("a", c("a", "b", "c", "d"))
class(grep_result)
1
[1] "integer"
1
grep_result
1
[1] 1
1
2
grepl_result <- grepl("a", c("a", "b", "c", "d"))
class(grepl_result)
1
[1] "logical"
1
grepl_result
1
[1]  TRUE FALSE FALSE FALSE

grep returns a integer vector (index).
grepl returns a logical vector (boolean).

Summary for grep and grepl

  grep grepl
Return integer index logical

References:

RDocumentation grep: Pattern Matching and Replacement

Trending Tags