简体   繁体   English

Usng R - gsub 在替换中使用代码 - 在模式后用句号替换逗号

[英]Usng R - gsub using code in replacement - Replace comma with full stop after pattern

I would like to manually correct a record by using R.我想使用 R 手动更正记录。 Last name and first name should always be separated by a comma.姓和名应始终用逗号分隔。

names <- c("ADAM, Smith J.", "JOHNSON. Richard", "BROWN, Wilhelm K.", "DAVIS, Daniel")

Sometimes, however, a full stop has crept in as a separator, as in the case of "JOHNSON. Richard".然而,有时,句号作为分隔符悄悄进入,例如“JOHNSON.Richard”。 I would like to do this automatically.我想自动执行此操作。 Since the last name is always at the beginning of the line, I can simply access it via sub :由于姓氏总是在行首,我可以简单地通过sub访问它:

sub("^[[:upper:]]+\\.","^[[:upper:]]+\\,",names)

However, I cannot use a function for the replacement that specifically replaces the full stop with a comma.但是,我不能使用 function 来专门用逗号替换句号。

Is there a way to insert a function into the replacement that does this for me?有没有办法将 function 插入为我做这件事的替代品中?

Your sub is mostly correct, but you'll need a capture group (the brackets and backreference \\1 ) for the replacement.您的sub大部分是正确的,但您需要一个捕获组(括号和反向引用\\1 )进行替换。

test_names <- c("ADAM, Smith J.", "JOHNSON. Richard", "BROWN, Wilhelm K.", "DAVIS, Daniel")

sub("^([[:upper:]]+)\\.","\\1\\,",test_names)
[1] "ADAM, Smith J."    "JOHNSON, Richard"  "BROWN, Wilhelm K."
[4] "DAVIS, Daniel"   

Can be done by a function like so:可以通过 function 来完成,如下所示:

names <- c("ADAM, Smith", "JOHNSON. Richard", "BROWN, Wilhelm", "DAVIS, Daniel")

replacedots <- function(mystring) {
  gsub("\\.", ",", names)
}
replacedots(names)
[1] "ADAM, Smith"      "JOHNSON, Richard" "BROWN, Wilhelm"   "DAVIS, Daniel"  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM