@Akrun
this is the output when i run code...and when i type View(df)
i get the old dataset.
> df %>%
+ group_by(ID) %>%
+ mutate(difftime=c(15, format(.POSIXct(diff(TIMESTAMP)), '%M:%S')))
Source: local data frame [192 x 9]
Groups: ID
TIMESTAMP ID STATUS ACTIVE PSET
1 2014-08-05 00:00:00 28808 DOWN 67.410 70
2 2014-08-05 00:15:00 28808 DOWN 67.410 70
3 2014-08-05 00:30:00 28808 DOWN 67.410 70
4 2014-08-05 00:45:00 28808 DOWN 67.410 70
5 2014-08-05 01:00:00 28808 DOWN 67.410 70
.. ... ... ... ... ...
Variables not shown: STAT (chr), PIN (chr), ONOFF (chr), difftime (chr)
I need to find the timestamp difference for each ID making the first timestamp for each ID as NA.
I have tried diff
but i am not able to find difference for each ID.
this can be made a little simpler by order
but how make R's diff function
know that ID's can be different
Below is the sample data for your reference.
Here is the code i am using as of now as requested.
foreach(i=1:length(xyzID)) %dopar%{
xyz$timediff<-c(15,diff(xyz$TIMESTAMP))
}
TIMESTAMP ID timediff
07/29/2014 10:15 AM 189252 NA
07/29/2014 10:45 AM 189252 0:30
07/29/2014 11:00 AM 189252 0:15
07/29/2014 11:15 AM 189252 0:15
07/29/2014 11:30 AM 302859 NA
07/29/2014 11:45 AM 302859 0:15
07/29/2014 04:00 PM 302859 04:15
07/29/2014 12:15 PM 189252 01:00
07/29/2014 12:30 PM 189252 0:15
07/29/2014 01:00 PM 189252 0:30
07/29/2014 01:15 PM 189252 0:15
07/29/2014 01:30 PM 178502 NA
07/29/2014 01:45 PM 178502 0:15