jeudi 22 septembre 2016

nested If statement on dates

I have a dataframe df which is as follows.

Id     ProcessDate
10     2011-12-29 14:14:00
11     2011-12-29 14:16:00
12     2011-12-29 14:14:00
13     2011-12-29 14:20:00
14     2011-12-29 14:49:00
15     2011-12-29 14:51:00
16     2011-12-29 14:53:00
17     2011-12-29 15:11:00
18     2011-12-29 15:13:00 
19     2011-12-29 15:10:00
20     2011-12-29 15:21:00
21     2011-12-29 14:34:00
22     2011-12-29 15:26:00  

I am trying to create a third column Status that will contain either one of these three values {Before, during , after } based on this condition.

 if  (df$ProcessDate < 2011-12-29 14:48:00)
 then  df$Status = "Before"
 else if (df$ProcessDate > 2011-12-29 14:48:00 & df$ProcessDate < 2011-12-29 15:16:00)
 then  df$Status = "Between"
 else  df$Status = "After"

The final dataframe should look like this.

Id     ProcessDate              Status
10     2011-12-29 14:14:00      Before
11     2011-12-29 14:16:00      Before
12     2011-12-29 14:14:00      Before
13     2011-12-29 14:20:00      Before
14     2011-12-29 14:49:00      Between
15     2011-12-29 14:51:00      Between       
16     2011-12-29 14:53:00      Between
17     2011-12-29 15:11:00      Between
18     2011-12-29 15:13:00      Between
19     2011-12-29 15:10:00      Between
20     2011-12-29 15:21:00      After
21     2011-12-29 14:34:00      After
22     2011-12-29 15:26:00      After

I tried few things and it didn't work, any help on this issue is much appreciated.

Aucun commentaire:

Enregistrer un commentaire