Since ifelse() strips attributes, I use multiple steps to replace ifelse() when it comes to date.
For example,
df <- data.table(a = (1:4),
b = as.Date(c("2012-05-05","2014-05-02","2016-01-02","2011-01-02")),
c = as.Date(c("2014-02-05","2010-01-02","2015-02-02","2012-03-02")))
year <- 2013
df[, d := as.Date(paste0(format(c, "%Y"), "-", format(b, "%m-%d")))]
df[d <= c, d := as.Date(paste0(year, "-", format(b, "%m-%d")))]
The example above is quite simple. In real life, I have a more complex situation, which requires making comparison using 3 date columns, totaling 5 different scenarios. Does it mean I need to use 5 steps to complete all the "if else" scenarios? If this is the case, I guess, the advantages of data.table are not best utilized.
Is there some way to avoid using multiple steps?
The codes up there is not quite confusion. Sorry for that. The purpose is that, if the combination of month and day of column b is earlier than that of column c, create a date with the year of 2013, month and day from column b; otherwise, create a date with the year of column c, month and day from column b.
Thanks to @docendo discimus, I changed the code.
One more example
year1<-2020
year2<-2025
year3<-2030
df<-data.table(a=(1:4),b=as.Date(c("2012-05-05","2014-01-02","2016-10-02","2011-01-02")),
c=as.Date(c("2014-09-05","2010-07-02","2015-02-02","2012-03-02")),
d=as.Date(c("2008-02-06","2009-08-07","2011-04-04","2010-07-10")))
df[,e:=as.Date(paste0(format(c,"%Y"),"-",format(b,"%m-%d")))];
df[e<=c & e>d,e:=as.Date(paste0(year1,"-",format(b,"%m-%d")))]
df[as.Date(paste0(format(c,"%Y"),"-",format(b,"%m-%d")))<=c & as.Date(paste0(format(d,"%Y"),"-",format(b,"%m-%d")))<=d,e:=as.Date(paste0(year2,"-",format(b,"%m-%d")))]
df[as.Date(paste0(format(c,"%Y"),"-",format(b,"%m-%d")))>c,e:=as.Date(paste0(year3,"-",format(b,"%m-%d")))]
The purpose of the above example is make comparison using 3 date columns. When I say compare, I mean use month and day only, regardless year.
If b<=c and b>d, change the year to 2020,
if b<=c and b<=d, change the year to 2025,
if b>c, change the year to 2030.
I need to use 4 steps to accomplish this. Step 3 and Step 4 becomes ugly since I changed the year of column e in step 2, I cannot use column e to compare with c and d anymore. Is there some way to simplify the above example?
as.Dateandformat? It makes your example unreadable. Anyway, please provide a representative, minimal reproducible example. For you example here (i.e., a simpleif/elsesituation), such a two-step approach is exactly what I use. - Rolandsprintfwould make this a whole lot more readableas.Date(sprintf('%s-%s-%s', format(c,"%Y"), format(b,"%m"),format(b,"%d")))- rawrdf[,d:=as.Date(paste0(format(c,"%Y"),"-",format(b,"%m-%d")))];df[d<=c,d:=as.Date(paste0(year,"-",format(b,"%m-%d")))]- talat