Naveed Khan Wazir
Naveed Khan Wazir

Reputation: 195

How to find the difference between two rows of data consisting of date and time in R

I am working with a data file consisting of one column of date and time as

df
    time_stamp
1 2003-09-06T20:21:51Z  (2003-09-06--> year-month-days-hours-mintutes-seconds)
2 2003-09-06T20:22:36Z
3 2003-09-06T20:22:51Z
4 2003-09-06T20:23:06Z
5 2003-09-06T20:24:56Z
6 2003-09-06T20:25:06Z

I want to find the difference between the two rows based on unit of minutes. I have applied this code to 1st convert the data into a date format, but it gives me an error like:

 x<-as.Date(df,format='%d/%b/%Y:%H:%M:%S')
 Error in as.Date.default(datew, format = "%d/%b/%Y:%H:%M:%S") : 
  do not know how to convert 'datew' to class “Date”

while the mode(df) shows a results of "list"

Any help will highly be appreciated. My output should look like this as:

 df
    time_stamp                   time_duration
1 2003-09-06T20:21:51Z              0
2 2003-09-06T20:22:36Z              1
3 2003-09-06T20:22:51Z              0.25  
4 2003-09-06T20:23:06Z              0.25
5 2003-09-06T20:24:56Z              1.8
6 2003-09-06T20:25:06Z              0.17

Thanks, in advance.

Upvotes: 2

Views: 322

Answers (1)

dwcoder
dwcoder

Reputation: 488

You seem to be passing a list to the as.Date function, and it doesn't know how to deal with lists. Try the following.

First I create your data

time_stamp <- 
c(
 "2003-09-06T20:21:51Z",             
 "2003-09-06T20:22:36Z",             
 "2003-09-06T20:22:51Z",             
 "2003-09-06T20:23:06Z",             
 "2003-09-06T20:24:56Z",             
 "2003-09-06T20:25:06Z")

df <- list(time_stamp=time_stamp)

Now we turn it into a POSIX date. Note that my format="%Y-%m-%dT%H:%M:%SZ" argument differs from yours, since yours produced an error when I tried it. The format has to reflect the nature of your date string. See help(strptime) for details.

x<-as.POSIXct(df$time_stamp,format="%Y-%m-%dT%H:%M:%SZ" , tz="GMT")

# Do the comparison as @akrun suggested
time_duration <- difftime(x[-1], x[-length(x)], unit='mins')
# Add a zero to the start 
time_duration <- c(0 , time_duration)

# Make your dataframe
df <- data.frame( time_stamp=df$time_stamp , time_duration=time_duration ) 

And the result is what you wanted:

> df
            time_stamp time_duration
1 2003-09-06T20:21:51Z     0.0000000
2 2003-09-06T20:22:36Z     0.7500000
3 2003-09-06T20:22:51Z     0.2500000
4 2003-09-06T20:23:06Z     0.2500000
5 2003-09-06T20:24:56Z     1.8333333
6 2003-09-06T20:25:06Z     0.1666667

Upvotes: 3

Related Questions