我有一串带连字符的字符串。我想删除连字符并将以下字母转换为小写,同时保持所有其他字母不变。您如何在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"

最佳答案

试试这个:

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

或这个:
> library(gsubfn)
> gsubfn("-(\\w)", tolower, test)
[1] "Kwak Minjung"

10-07 13:48