Hi,

Internally, once you have a Date class object in R, the "printed" output 
displayed will be the default, which I believe is influenced by your locale. 
See ?format.Date.

That being said, in your example data below, 07022020, could be either July 2, 
2020, or February 7, 2020. How do you know which one is correct, since both are 
legal conversions?

Thus, you need some other flag value to determine which conversion is correct.

Can you use the observer values as a flag?

If so, then you can use a conditional statement (e.g. ?ifelse) to make the 
conversion.

For example:

  d$date <- ifelse(d$observer %in% c(vector, of, observers), 
                   as.Date(d$date, format = "%d%m%Y"),
                   as.Date(d$date, format = "%m%d%Y"))

Also, you might not want to overwrite the original values, and create a new 
column, in the case of errors.

Regards,

Marc Schwartz


> On Jan 20, 2021, at 11:08 AM, krissievdh <krissie...@gmail.com> wrote:
> 
> Hi,
> 
> I have a big database where one-third of the data is in a different date
> format than the rest. I'll add an example table to show you.
> 
> | plot         | observer          | date            |
> | 1             | K                      | 31012020  |
> | 2             | K                      | 07022020  |
> | 3             | B                      | 01282020  |
> | 4             | B                      | 01292020  |
> So I have two different date formats; the first is in d m y while the other
> one is in m d y.
> 
> My question is how can I change all the date data into the same format?
> Preferably in dd/mm/yy. (31-01-2020)
> I know I could use:
> d$date <- as.Date(d$date, format = "%d%m%Y")
> d$date <- as.Date(d$date, format = "%m%d%Y")
> 
> but I can only do one and then it doesn't work. Is there a way i can use
> the first line for the date of observer K and the other line for the date
> of observer B?
> 
> Thanks

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to