0

I got this problem. I have a data frame that has a date column. It's in characters, not date format. It has this ridiculous format that wrote as "yyyy.mm.dd". Yes you are reading it right, it has "." between year, month and day. Now I have to turn it into yyyy-mm-dd form and turn its data type to date. But the problem is that r doesn't recognise this form and won't let me turn it into the date data type. So I try to change the "." into "-" by using the code below:

recovered$Date <- str_replace_all(recovered$Date, '.', '-')

Or

recovered$Date <- gsub('.', '-', recovered$Date)

But the issue is, both of those codes turned every character in my date column into "-". So instead of turning "2020.01.01" into "2020-01-01", they were turned into "----------" for everyone in that column. Please help.

Joel
  • 1
  • You need to use `gsub('\\.', '-', recovered$Date)` the `.` is a special character in a regular expression meaning match any character. To search for a period using a regular expression, you need to "escape" it. In R, you can do this by putting `"\\"` before whatever you want to escape. – DaveArmstrong May 14 '22 at 02:49

0 Answers0