Intermediate R
Filip Schouwenaars
DataCamp Instructor
grep()
, grepl()
sub()
, gsub()
animals <- c("cat", "moose", "impala", "ant", "kiwi")
grepl(pattern = <regex>, x = <string>)
grepl(pattern = "a", x = animals)
TRUE FALSE TRUE TRUE FALSE
grepl(pattern = "^a", x = animals)
FALSE FALSE FALSE TRUE FALSE
grepl(pattern = "a$", x = animals)
FALSE FALSE TRUE FALSE FALSE
?regex
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
which(grepl(pattern = "a", x = animals))
1 3 4
grep(pattern = "^a", x = animals)
4
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"
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"
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