intro_r/doc/match.patt.org

857 B
Executable File

Matching patterns introduction using R: grep, gsub, regex

  x = c(40, 30, 20, 44, 54)
  y = paste0(x, "L")
  y
[1] "40L" "30L" "20L" "44L" "54L"

gsub function

Remove the first component of each string

  gsub("^.{1}", "", y)
[1] "0L" "0L" "0L" "4L" "4L"

Remove the last component of each string

  gsub(".{1}$", "", y)
[1] "40" "30" "20" "44" "54"
sub(".{1}$", "", y)
[1] "40" "30" "20" "44" "54"
  noquote(y)
[1] 40L 30L 20L 44L 54L