如何替换数据框列中的字符串?

时间:2018-02-16 15:55:11

标签: r string

我有一个包含这些字符串的列的数据框:

Rear-Naked Choke 
Submission (Rear-Naked Choke 
Rear Naked Choke

示例:

                  Method
1             Rear-Naked Choke
2             Rear Naked Choke
3             Rear Naked Choke
4 Submission (Rear-Naked Choke
5             Rear Naked Choke
6             Rear Naked Choke

我希望他们都是"Rear-Naked Choke",以便我可以统计一个类别。换句话说,我只想让整个列读取"Rear-Naked Choke"

期望的输出:

                  Method
1             Rear-Naked Choke
2             Rear-Naked Choke
3             Rear-Naked Choke
4             Rear-Naked Choke
5             Rear-Naked Choke
6             Rear-Naked Choke

我尝试了什么:

  

str_replace_all(c(" Rear-Naked Choke" =" Rear-Naked Choke","提交   (后赤裸窒息)" ="后赤裸扼流圈","后赤裸扼流圈)" =   "后赤裸窒息"))

  

gsub("提交(后赤裸扼流圈)","后赤裸窒息",   数据$列)

当我想要替换"("时,gsub无法正常工作。

你知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

为了替换R中的特殊字符,你必须这样做:

gsub("\\(","","(hello") #This will give you "hello"

我对你的问题的推荐是首先用这样的gsub摆脱每个特殊的chacrater:

df$mycol <- gsub("[[:punct:]]","",df$mycol)

之后,您将更容易替换列中的任何字符串。

在R控制台中键入?regex以获取更多信息。