r - Adding column with condition using data.table -
r - Adding column with condition using data.table -
suppose have next data.table
:
date=c("2014-02-06","2014-02-06","2014-03-01","2014-03-01","2014-03-28","2014-04-25","2014-04-25") departure=c("ny", "ny", "doha", "tokyo", "paris", "tokyo", "tokyo") arrival=c("milano", "beijing", "moscow", "moscow", "singapore", "yaounde", "milano") dt<-data.table(date, departure, arrival)
giving result:
date departure arrival 1: 2014-02-06 ny milano 2: 2014-02-06 ny beijing 3: 2014-03-01 doha moscow 4: 2014-03-01 tokyo moscow 5: 2014-03-28 paris singapore 6: 2014-04-25 tokyo yaounde 7: 2014-04-25 tokyo milano
now have date:
lawdate="2014-03-17"
and want add together column named "law" in dt such if date>lawdate
it contains true, false otherwise. doing moment not know if efficient way ... since table dt extremely big ...
my current solution ... not know if there improve way data.table ..
dt$law = ifelse(date>lawdate, true, false)
dt[, law:=false][date>lawdate, law:=true]
or shorter
dt[, law:=date>lawdate]
kind greetings
r data.table
Comments
Post a Comment