Replacing all occurrences of a pattern in a string

耗尽温柔 提交于 2020-01-28 05:11:59

问题


Used to run R with numbers and matrix, when it comes to play with strings and characters I am lost. I want to analyze some data where the time is read into R as follow:

>my.time.char[1]
[1] "\"2011-10-05 15:55:00\"" 

I want to end up with a string containing only:

"2011-10-05 15:55:00"

Using the function sub() (that i barely understand...), I got the following result:

> sub("(\")","",my.time.char[1])
[1] "2011-10-05 15:55:00\""

This is closer to the format i am looking for, but I still need to get rid of the two last characters (\").


回答1:


The second line from ?sub explains:

sub and gsub perform replacement of the first and all matches respectively.

which should tell you to use gsub instead.



来源:https://stackoverflow.com/questions/10560503/replacing-all-occurrences-of-a-pattern-in-a-string

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