13.1 Find simple matches with grep
- Find a pattern anywhere in the string (outputs the index of the element):
# By default, outputs the index of the element matching the pattern
grep(pattern="Gen",
x="Genomics")## [1] 1
- Show actual element where the pattern is found (instead of the index only) with value=TRUE:
## [1] "Genomics"
- Non case-sensitive search with ignore.case=TRUE:
# Enter the pattern in lower-case, but case is ignored
grep(pattern="gen",
x="Genomics",
value=TRUE,
ignore.case=TRUE)## [1] "Genomics"
- Show if it DOESN’T match the pattern with inv=TRUE:
# Shows what doesn't match
grep(pattern="gen",
x="Genomics",
value=TRUE,
ignore.case=TRUE,
inv=TRUE)## character(0)