Ronak Shah
Ronak Shah

Reputation: 388862

Why does the gsub function in R doesn't work on '.' operator?

I have an array named myfile which has all the dates in character format. For eg - "2014.01.29" "2014.02.02" "2014.01.09" "2014.01.23" "2014.01.09" "2014.01.29"

Now, I want to replace this '.' operator to '-'. So I want "2014.01.29" to be like "2014-01-29". When I use the code

 gsub('.' ,  '-' ,  myfile[1])

I get the output as '----------'. The command works absolutely normal when I replace '.' in gsub with anything else. Any help would be appreciated.

Upvotes: 0

Views: 2251

Answers (1)

akrun
akrun

Reputation: 887028

You need to escape the . which can be done either putting it in a [.] or \\..

  gsub('[.]', '-', myfile[1])

or

 gsub('\\.', '-', myfile[1])

Upvotes: 5

Related Questions