courty340
courty340

Reputation: 131

Changing date formats and calculating duration using lubridate

I am trying to get the duration between issue_d and last_pymnt_d in my dataset using the lubridate package. issue_d is in the following formatting chr "2015-05-01T00:00:00Z" and last_pymnt_d is in chr "Feb-2017". I need them in the same format (just need "my" or "myd" is fine if "my" is not an option) Then I need to know calculate between issue_d and last_pymnt_d.

lcDataSet2$issue_d<-parse_date_time(lcDataSet2$issue_d, "myd")

turns my issue_d into NA. I also get the below error when even just trying to view last_pymnt_d in date format

as.Date(lcRawData$last_pymnt_d)
    Error in charToDate(x) : 
    character string is not in a standard unambiguous format

How can I get these into the same date format and then calculate the duration?

Upvotes: 1

Views: 141

Answers (2)

Ben Norris
Ben Norris

Reputation: 5747

The zoo package gives you a function as.yearmon to covert dates into yearmon objects containing only the month and year. Since your last_pymnt_d is only month and year, the best date difference you will get is number of months:

library(zoo)
issue_d <- "2015-05-01T00:00:00Z"
last_pymnt_d <- "Feb-2017"

diff <- as.yearmon(last_pymnt_d, format = "%b-%Y") - as.yearmon(as.Date(issue_d))
diff
1.75

Under the hood, the yearmon object is a number of years, with the decimal component representing the months. A difference in yearmon of 1.75 is 1 year and 9 months.

diff_months <- paste(round(diff * 12, 0), "months")
"21 months"
diff_yearmon <- paste(floor(diff), "years and", round((diff %% 1) * 12, 0), "months")
diff_yearmon
"1 years and 9 months"

Upvotes: 2

manotheshark
manotheshark

Reputation: 4367

The order and letter case of the format string is important for parsing dates.

library(lubridate)

parse_date_time('2015-05-01T00:00:00Z', 'Y-m-d H:M:S')
[1] "2015-05-01 UTC"

parse_date_time('Feb-2017', 'b-Y')
[1] "2017-02-01 UTC"

If wanting just the month and year there is a zoo function

library(zoo)

date1 <- as.yearmon('2015-05-01T00:00:00Z')
[1] "May 2015"

date2 <- as.yearmon('Feb-2017', '%b-%Y')
[1] "Feb 2017"

difftime(date2, date1)
Time difference of 642 days

Upvotes: 2

Related Questions