我有一个包含这些字符串的列的数据框:
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无法正常工作。
你知道如何解决这个问题吗?
答案 0 :(得分:0)
为了替换R中的特殊字符,你必须这样做:
gsub("\\(","","(hello") #This will give you "hello"
我对你的问题的推荐是首先用这样的gsub
摆脱每个特殊的chacrater:
df$mycol <- gsub("[[:punct:]]","",df$mycol)
之后,您将更容易替换列中的任何字符串。
在R控制台中键入?regex
以获取更多信息。