a question from R newbie user: I have a data with consecutive enabled_datetime
and disabled_datetime
as shown below:
x<-as.data.frame(cbind(
supplier_id=281743,
enabled_datetime=c('2016-06-13 13:31:02','2016-06-14 07:39:19','2016-06-14 12:36:03','2016-06-16 13:44:30','2016-06-17 06:42:14'),
disabled_datetime = c('2016-06-14 07:39:19','2016-06-14 12:36:03','2016-06-16 13:44:30','2016-06-17 06:42:14', NA),
discount=c(25,15,15,10,30))
)
x
supplier_id enabled_datetime disabled_datetime discount
281743 2016-06-13 13:31:02 2016-06-14 07:39:19 25
281743 2016-06-14 07:39:19 2016-06-14 12:36:03 15
281743 2016-06-14 12:36:03 2016-06-16 13:44:30 15
281743 2016-06-16 13:44:30 2016-06-17 06:42:14 10
281743 2016-06-17 06:42:14 <NA> 30
What I'd like to transform into is like this:
supplier_id enabled_datetime disabled_datetime discount
281743 2016-06-13 13:31:02 2016-06-14 07:39:19 25
281743 2016-06-14 07:39:19 2016-06-16 13:44:30 15
281743 2016-06-16 13:44:30 2016-06-17 06:42:14 10
281743 2016-06-17 06:42:14 <NA> 30
i.e. merge rows with the same supplier_id
, discount
and have consecutive enabled_datetime
and disabled_datetime
. What I can think of is to use for
loop, any one know how to do so different way? Thanks in advance.