How to replace a letter with a lower case after finding it with a regular expression in R

I have a bunch of hyphenated lines. I want to remove the hyphen and convert the next letter to lowercase, while retaining all the other letters. How to complete a task in R?

test <- "Kwak Min-Jung"
gsub(x=test,pattern="-(\\w)",replacement="\\1")
# [1] "Kwak MinJung"  , Not what I want
# I want it to convert to  "Kwak Minjung"
+3
source share
2 answers

Try the following:

> gsub("-(\\w)", "\\L\\1", test, perl = TRUE)
[1] "Kwak Minjung"

or that:

> library(gsubfn)
> gsubfn("-(\\w)", tolower, test)
[1] "Kwak Minjung"
+4
source

Use \\Lor \\Uto change case in the replace argument. You can use \\Eto complete the case conversion action.

gsub(x=test,pattern="-(\\w)",replacement="\\L\\1", perl=TRUE)
# [1] "Kwak Minjung"
+2
source

All Articles