Skip to content

Help with changing date format in R

4 messages · krissievdh, Sarah Goslee, Jeff Newmiller +1 more

#
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
#
Hi,

If the date format is determined by observer, you could for instance
use subset to divide it into two data frames, fix the dates and
recombine, or use ifelse to use the correct format based on observer.
This is a basic data manipulation task, and there are lots of ways
approach it.

Sarah
On Wed, Jan 20, 2021 at 1:32 PM krissievdh <krissievdh at gmail.com> wrote:

  
    
#
Perhaps

d$date <- as.Date(d$date, format = ifelse("K"==d$observer, "%d%m%Y", "%m%d%Y"  ))
On January 20, 2021 8:08:33 AM PST, krissievdh <krissievdh at gmail.com> wrote:

  
    
#
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