r - Strange behavior involving dates - "origin must be supplied" -
i have data.table so
dt <- data.table(x=as.date(c("2014-1-1", "2015-1-1", "2016-1-1")), y=as.date(c(na, "2015-6-1", na))) dt x y 1: 2014-01-01 <na> 2: 2015-01-01 2015-06-01 3: 2016-01-01 <na> i want add column z equal y y not na, , x otherwise.
dt[, z:=ifelse(is.na(y), x, y)] dt x y z 1: 2014-01-01 <na> 16071 2: 2015-01-01 2015-06-01 16587 3: 2016-01-01 <na> 16801 but reason above statement casts z numeric. if try convert date as.date error
dt[, z:=as.date(ifelse(is.na(y), x, y))] error in as.date.numeric(ifelse(is.na(y), x, y)) : 'origin' must supplied what gives , how accomplish i'm trying do?
when r looks @ dates integers, origin january 1, 1970.
http://www.ats.ucla.edu/stat/r/faq/dates.htm
dt[, z:=as.date(ifelse(is.na(y), x, y), origin="1970-01-01")] update: frank suggests, seems work , not seem require un-coercion: dt[, z:=replace(y, is.na(y), x)]. throws warning use w/ caution.
Comments
Post a Comment