Re-format date and time in R

36 Views Asked by At

I have two columns of data (both dates), and I need to find the number of days in between (basically the difference between the columns).

One of the columns is formatted like this (imported from SPSS) - I only included two rows as example: 12-Mar-2020 15:51:32 06-Apr-2019 17:08:03

What I need this to look like:

  1. I need a new column that doesn't have the time, and it only includes the dates (i.e. get rid of 15:51:32 and 17:08:03).
  2. I need to reformat the dates as numbers in the order of year-month-date so that it matches the format of my other column (i.e. I need Mar to become 03 and Apr to 04, in addition to changing the order).

I tried using lubridate package, but it was not successful because of how this column is formatted (in case it's helpful, the class of this column is currently "character"). Please note: My data set is quite big, so I appreciate a solution that works on the entire column at once.

I would appreciate your help on this.

1

There are 1 best solutions below

0
Jon Spring On BEST ANSWER
library(lubridate)
as_date(dmy_hms(c("12-Mar-2020 15:51:32", "06-Apr-2019 17:08:03")))

Result

[1] "2020-03-12" "2019-04-06"