在r中改变字符串中单词的位置

时间:2015-05-05 14:36:15

标签: regex r string

我有一个字符串向量,如下所示:

> string_vec
 [1] "XXX"                     "Snakes On A Plane"       "Mask of the Ninja"       "Ruslan"                 
 [5] "Kill Switch"             "Buddy Holly Story, The"  "Believers, The"          "Closet, The"            
 [9] "Eyes of Tammy Faye, The" "Gymnast, The"            "Hunger, The" 

最后有一些名字包含“,The”。我想删除逗号和空格,并在所有其他文本之前移动“The”。

例如:“Buddy Holly Story,”成为“The Buddy Holly Story”。

使用模式隔离记录很简单:

string_vec[grepl("[Aa-zZ]+, The", string_vec) == TRUE]

我现在如何调整位置?

数据

string_vec <- c("XXX", "Snakes On A Plane", "Mask of the Ninja", 
"Ruslan", 
"Kill Switch", "Buddy Holly Story, The", "Believers, The", 
"Closet, The", 
"Eyes of Tammy Faye, The", "Gymnast, The", "Hunger, The")

1 个答案:

答案 0 :(得分:8)

您可以尝试

sub('^(.*), The', 'The \\1', string_vec)
#[1] "XXX"                    "Snakes On A Plane"      "Mask of the Ninja"     
#[4] "Ruslan"                 "Kill Switch"            "The Buddy Holly Story" 
#[7] "The Believers"          "The Closet"             "The Eyes of Tammy Faye"
#[10] "The Gymnast"            "The Hunger"