简体   繁体   English

gsub用退格替换模式

[英]gsub replace pattern with backspace

I have a data set with a column that contains a label with the year ( OldLabel ), and I want to make another column that contains only the label, not the year ( NewLabel ). 我有一个数据集,该数据集的列包含带有年份的标签( OldLabel ),我想制作另一列仅包含标签而不包含年份( NewLabel )的列。 I wrote the following code, but it leaves a space at the end of the new labels. 我编写了以下代码,但是在新标签的末尾保留了一个空格。

data["NewLabel"] <- gsub("20..", "", data$OldLabel)
#removes any part of the OldLabel column that starts with 20 and ends with 2 digits, e.g: 2011 or 2008

Is there a way to have gsub replace the sequence with a backspace, so it gets rid of any spaces around the year it replaces? 有没有一种方法可以让gsub用退格键替换序列,以便它去除替换后一年中的所有空格? I tried using "\\\\b" as my replacement text, but that just replaced it with b , not a backspace. 我尝试使用"\\\\b"作为替换文本,但是只是将其替换为b ,而不是退格键。

EDIT: Per request, an example of OldLabel would be "Valley Summer 2014" , which should become "Valley Summer" , but ends up being "Valley Summer " with my current code. 编辑:根据请求, OldLabel的示例将是"Valley Summer 2014" ,应OldLabel "Valley Summer" ,但最终以我当前的代码成为"Valley Summer " However, some might also be of the form 2012 Valley Summer , so I don't think simply including a space in the pattern would be robust enough. 但是,有些格式也可能是2012 Valley Summer ,所以我认为在模式中仅包含一个空格并不足够健壮。

Try this: 尝试这个:

 data["NewLabel"] <- gsub("[ ]{0,1}20[[:digit:]]{2}[ ]{0,1}", "", data$OldLabel)

The paired curley-braces are repetition quantifiers that have a range determined by either one (exact) or two (min and max) values. 成对的柯利括号是重复量词,其范围由一个(精确)或两个(最小和最大)值确定。 See ?regex for more details. 有关更多详细信息,请参见?regex (You don't want to replace them with backspace characters.) (您不想将它们替换为退格字符。)

test <- c("2012 Valley Summer", "Valley Summer 2014")
gsub("[ ]{0,1}20[[:digit:]]{2}[ ]{0,1}", "", test)
#[1] "Valley Summer" "Valley Summer"
data["NewLabel"] <- gsub("\\s*[0-9]\\s*", "", data$OldLabel)

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

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