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"
Try the following:
> gsub("-(\\w)", "\\L\\1", test, perl = TRUE) [1] "Kwak Minjung"
or that:
> library(gsubfn) > gsubfn("-(\\w)", tolower, test) [1] "Kwak Minjung"
Use \\Lor \\Uto change case in the replace argument. You can use \\Eto complete the case conversion action.
\\L
\\U
\\E
gsub(x=test,pattern="-(\\w)",replacement="\\L\\1", perl=TRUE) # [1] "Kwak Minjung"