Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time difference in R

Tags:

r

I have some data which looks like this:

id    time
1     2013-02-04 02:20:59
1     2013-02-04 02:21:05
1     2013-02-04 02:21:24
2     2013-02-04 02:21:26
2     2013-02-04 02:22:19
2     2013-02-04 02:22:35

I want to take the difference in time for each id between two time values such as for:

id 1 02:21:05-02:20:59=00:00:06. 

How can I do this in R?

like image 819
Sanjana Haralalka Avatar asked Aug 09 '26 10:08

Sanjana Haralalka


1 Answers

You should do diff on time as well as id and then using ifelse populate the third column

df <- structure(list(id = c(1L, 1L, 1L, 2L, 2L, 2L), 
        time = structure(c(1359915659, 1359915665, 1359915684, 
        1359915686, 1359915739, 1359915755), class = c("POSIXct", 
        "POSIXt"), tzone = "")), .Names = c("id", "time"), row.names = c(NA, -6L), 
        class = "data.frame")
df
##   id                time
## 1  1 2013-02-04 02:20:59
## 2  1 2013-02-04 02:21:05
## 3  1 2013-02-04 02:21:24
## 4  2 2013-02-04 02:21:26
## 5  2 2013-02-04 02:22:19
## 6  2 2013-02-04 02:22:35

## here you are checking if that result is diff in time only when diff in id is 0
df$result <- c(0, ifelse(diff(df$id) == 0, diff(df$time), 0))

df
##   id                time result
## 1  1 2013-02-04 02:20:59      0
## 2  1 2013-02-04 02:21:05      6
## 3  1 2013-02-04 02:21:24     19
## 4  2 2013-02-04 02:21:26      0
## 5  2 2013-02-04 02:22:19     53
## 6  2 2013-02-04 02:22:35     16
like image 100
CHP Avatar answered Aug 10 '26 23:08

CHP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!