Replace dots using `gsub`

被刻印的时光 ゝ 提交于 2020-05-09 06:30:14

问题


I am trying to replace all the "." in a specific column of my data frame with "/". There are other characters in each cell and I want to make sure I only change the "."'s. When I use gsub, I get an output that appears to make the changes, but then when I go to View(), the changes are not actually made...I thought gsub was supposed to actually change the value in the data frame. Am I using it incorrectly? I have my code below.

gsub(".", "/", spy$Identifier, ignore.case = FALSE, perl = FALSE,
    fixed = TRUE, useBytes = FALSE)

I also tried sub, but the code I have below changed every entry itself to "/" and I am not sure how to change it.

spy$Identifier <- sub("^(.).*", "/", spy$Identifier)

Thanks!


回答1:


My recommendation would be to escape the "." character:

        spy$Identifier <- gsub("\\.", "/", spy$Identifier)

In regular expression, a period is a special character that matches any character. "Escaping" it tells the search to look for an actual period. In R's gsub this is accomplished with two backslashes (i.e.: "\\"). In other languages, it's often just one backslash.



来源:https://stackoverflow.com/questions/37709421/replace-dots-using-gsub

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!