R gsub()

1 개요[ | ]

R gsub()
  • "global + sub()"
  • "R Pattern Matching And Replacement"
Usage
gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,
     fixed = FALSE, useBytes = FALSE)
예시
gsub("([ab])", "\\1_\\1_", "abc and ABC") # [1] "a_a_b_b_c a_a_nd ABC"
## capitalizing
txt <- "a test of capitalizing"
gsub("(\\w)(\\w*)", "\\U\\1\\L\\2", txt, perl=TRUE) # [1] "A Test Of Capitalizing"
gsub("\\b(\\w)",    "\\U\\1",       txt, perl=TRUE) # [1] "A Test Of Capitalizing"
txt2 <- "useRs may fly into JFK or laGuardia"
gsub("(\\w)(\\w*)(\\w)", "\\U\\1\\E\\2\\U\\3", txt2, perl=TRUE) # [1] "UseRS MaY FlY IntO JFK OR LaGuardiA"
sub("(\\w)(\\w*)(\\w)", "\\U\\1\\E\\2\\U\\3", txt2, perl=TRUE) # [1] "UseRS may fly into JFK or laGuardia"
haystack <- c("red", "blue", "green", "blue", "green forest")
# replaces pattern with replacement (once)
sub("e", "+", haystack) # [1] "r+d"          "blu+"         "gr+en"        "blu+"         "gr+en forest"
# replaces pattern with replacement (global)
gsub("e", "+", haystack)  # [1] "r+d"          "blu+"         "gr++n"        "blu+"         "gr++n for+st"

2 같이 보기[ | ]

3 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}