简体   繁体   English

在 R 中将“-”和大写字母中的字符串更改为无大写字母和空格

[英]Change string in "-" and capital letter to no capital letter and space in R

I am having some issues with a regular expression I hope you can help me with.我在使用正则表达式时遇到了一些问题,希望您能帮助我。 I have a dataset that looks like this:我有一个如下所示的数据集:

name <- c("Chester-le-Street",  
      "Westbury-on-Trym",
      "Easton-in-Gordano",  
      "Weston-super-Mare",
      "Bourne End-cum-Hedsor",
      "Amersham-on-the-Hill East",
      "South Westbury-on-Trym")

What I want to do is mainly two things:我想做的主要是两件事:

  1. remove the symbols "-"删除符号“-”
  2. Replace the first letter after where a "-" was by a capital letter.将“-”后面的第一个字母替换为大写字母。

In a way that would result in the following:在某种程度上会导致以下结果:

target_name <- c("Chester Le Street",   
          "Westbury On Trym",
          "Easton In Gordano",  
          "Weston Super Mare",
          "Bourne End Cum Hedsor",
          "Amersham On The Hill East",
          "South Westbury On Trym")

Been trying many different things that take very long and I never quite get exactly what I am going for nonetheless.一直在尝试许多不同的事情,这些事情需要很长时间,但我从来没有完全得到我想要的东西。 Would really appreciate any help.非常感谢任何帮助。 Thanks.谢谢。

You could use -(\w) as your pattern which is a dash followed by a letter.您可以使用-(\w)作为您的模式,它是一个破折号后跟一个字母。 Capture the letter as group 1, then replace that with a space and the Capital letter of the captured group 1. ie \U\1 .将字母捕获为第 1 组,然后将其替换为空格和捕获的第 1 组的大写字母。即\U\1 Note that to do this, we escape the \ hence have 2 backslash in the R code请注意,为此,我们转义\因此在 R 代码中有 2 个反斜杠

gsub("-(\\w)", " \\U\\1", name, perl = TRUE)
[1] "Chester Le Street"         "Westbury On Trym"          "Easton In Gordano"        
[4] "Weston Super Mare"         "Bourne End Cum Hedsor"     "Amersham On The Hill East"
[7] "South Westbury On Trym"  

Or:或者:

library(stringr)
str_to_title(str_replace_all(name, '-', ' '))
[1] "Chester Le Street"         "Westbury On Trym"          "Easton In Gordano"        
[4] "Weston Super Mare"         "Bourne End Cum Hedsor"     "Amersham On The Hill East"
[7] "South Westbury On Trym"  
library(stringr)

name <- c("Chester-le-Street",  
          "Westbury-on-Trym",
          "Easton-in-Gordano",  
          "Weston-super-Mare",
          "Bourne End-cum-Hedsor",
          "Amersham-on-the-Hill East",
          "South Westbury-on-Trym")

name %>%
  str_replace_all("-", " ") %>%
  str_to_title()

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

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