Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

r - Replacing all occurrences of a pattern in a string

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 (").

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...