Skip to content

Instantly share code, notes, and snippets.

@pratos
Last active April 4, 2017 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pratos/4e57fcfea5d7122d161917c152e5acf4 to your computer and use it in GitHub Desktop.
Save pratos/4e57fcfea5d7122d161917c152e5acf4 to your computer and use it in GitHub Desktop.
To convert mixed date strings to standard date format
library(lubridate)
parse_date_time(x = paste("01-", "13-Jan", sep = ""),
+                 orders = c("d m y", "d-y-m", "m-d-y"),
+                 locale = "eng", tz="GMT")
[1] "2013-01-01 GMT"

parse_date_time(x = paste("01-", "Jan-13", sep = ""),
+                 orders = c("d m y", "d-y-m", "m-d-y"),
+                 locale = "eng", tz="GMT")
[1] "2013-01-01 UTC"

Converting the issue_d and earliest_cr_line to Date format

fmts <- c("%d-%y-%B","%d-%B-%y")

earliest_cr_line <- as.Date(as.numeric(apply(outer(paste("01-", train_test$earliest_cr_line, sep = ""), fmts, as.Date), 1, na.omit)),"1970-01-01")
#train_test$earliest_cr_line <- as.POSIXct(earliest_cr_line, tz="GMT")

past <- function(x) ifelse(x > Sys.Date(), seq(from=x, length=2, by="-100 year")[2], x)
train_test$earliest_cr_line <- as.Date(sapply(earliest_cr_line, past), "1970-01-01")
#train_test$earliest_cr_line <- as.POSIXct(train_test$earliest_cr_line, tz="GMT")

#print ("The credit line::::::::::::::::::::::::::::::::")
#head(earliest_cr_line)
train_test$issue_d <- as.Date(train_test$issue_d, tz="GMT")
 
train_test$month_issued <- month(train_test$issue_d)
train_test$record_since_mths <- as.numeric(round((train_test$issue_d - train_test$earliest_cr_line)/12))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment