3

I am using "R" and am hoping someone can assist with my date formatting issue. I have a character variable from a dataset that I Imported from Excel.

DateVar <- c("12-07-2017", "43229", "43137", "03-27-2018")

The excel file I am using has two date formats in the same variable (MM-DD-YYYY and YYYY-MM-DD), hence the two formats in "DateVar". The date formatted YYYY-MM-DD converts to the excel date (i.e 43229).

I would like to have all the values be the same date format (ideally YYYY-MM-DD), but I am having issues converting them consistently.

Your help is much appreciated.

SteveM
  • 143
  • 10

1 Answers1

1

You can create an indicator vector for the observations that have been converted wrongly:

indicator <- !grepl("-", DateVar)

Then you can use this vector to convert these dates using the answer from this - How to convert Excel date format to proper date with Lubridate .

Shree
  • 9,963
  • 1
  • 11
  • 31
Mr. Z
  • 1,321
  • 9
  • 24
  • Interesting. So if I wanted to now apply the as.date function from the link you provided, how would I do that to only the indicator values that resulted in "TRUE"? – SteveM Nov 18 '18 at 21:30