Regular Expressions

Intermediate R

Filip Schouwenaars

DataCamp Instructor

Regular Expressions

  • Sequence of (meta)characters
  • Pattern existence
  • Pattern replacement
  • Pattern extraction
  • grep(), grepl()
  • sub(), gsub()
Intermediate R

grepl()

animals <- c("cat", "moose", "impala", "ant", "kiwi")
grepl(pattern = <regex>, x = <string>)
grepl(pattern = "a", x = animals)
TRUE FALSE  TRUE  TRUE FALSE
Intermediate R

grepl()

grepl(pattern = "^a", x = animals)
FALSE FALSE FALSE  TRUE FALSE
grepl(pattern = "a$", x = animals)
FALSE FALSE  TRUE FALSE FALSE
?regex
Intermediate R

grep()

animals <- c("cat", "moose", "impala", "ant", "kiwi")
grepl(pattern = "a", x = animals)
TRUE FALSE  TRUE  TRUE FALSE
grep(pattern = "a", x = animals)
1 3 4
Intermediate R

grep()

which(grepl(pattern = "a", x = animals))
1 3 4
grep(pattern = "^a", x = animals)
4
Intermediate R

sub(), gsub()

animals <- c("cat", "moose", "impala", "ant", "kiwi")
sub(pattern = <regex>, replacement = <str>, x = <str>)
sub(pattern = "a", replacement = "o", x = animals)
"cot"    "moose"  "impola" "ont"    "kiwi"
gsub(pattern = "a", replacement = "o", x = animals)
"cot"    "moose"  "impolo" "ont"    "kiwi"
Intermediate R

sub(), gsub()

animals <- c("cat", "moose", "impala", "ant", "kiwi")
sub(pattern = "a", replacement = "o", x = animals)
"cot"    "moose"  "impola" "ont"    "kiwi" 
gsub(pattern = "a", replacement = "o", x = animals)
"cot"    "moose"  "impolo" "ont"    "kiwi"
Intermediate R

sub(), gsub()

gsub(pattern = "a|i", replacement = "_", x = animals)
"c_t"    "moose"  "_mp_l_" "_nt"    "k_w_"
gsub(pattern = "a|i|o", replacement = "_", x = animals)
"c_t"    "m__se"  "_mp_l_" "_nt"    "k_w_"
Intermediate R

Let's practice!

Intermediate R

Preparing Video For Download...