Reputation: 51
I have a column of dates with some dates missing and having NA instead. Dates are in format of Date and expressed as: 2010-09-02
I am trying to convert if the cell has date in it to convert it to 1, and if NA to 0.
So far I have:
LoanData$DefaultDate[!is.na(as.Date(LoanData$DefaultDate, origin = "1970-01-01")) == TRUE] <- 1
and I keep receiving an error:
Error in as.Date.numeric(value) : 'origin' must be supplied
Googled the error, but still did not find any explanation. Any ideas?
Upvotes: 0
Views: 398
Reputation: 2435
as.Date will return NA
if it finds an NA or the format is wrong. Therefore you can just check whether it is an NA after transformation with is.na
. This returns a logical, which can be inverted with !
and transformed into integer with *1
v <- c("2010-09-02", NA, "2010-06-02")
(!is.na(as.Date(v)))*1
[1] 1 0 1
Most likely in your data:
LoanData$DefaultDate <- (!is.na(as.Date(LoanData$DefaultDate)))*1
Upvotes: 1