0

If I have some data like this.

 1  A   02-01-2015 08:08:00 

 1  B   02-01-2015 08:11:00 

 1  C   02-01-2015 08:12:00 

 1  D   02-01-2015 08:16:00 

 2  A   02-01-2015 09:08:00 

 2  B   02-01-2015 09:11:00 

 2  C   02-01-2015 09:13:00 

 2  D   02-01-2015 09:19:00

I want to get time difference each row in group. I expect result like this

 1  B   3:00

 1  C   1:00

 1  D   4:00

 2  B   3:00

 2  C   2:00

 2  D   6:00
ekad
  • 14,436
  • 26
  • 44
  • 46
DK2
  • 651
  • 1
  • 7
  • 34

1 Answers1

1

Plyr will likely work as well, but with base R, a couple tapply's will work.

Recreate data using numbers instead of dates.

x <- data.frame(groups = c(1,1,1,1,2,2,2,2),id = rep(LETTERS[1:4],2),data = c(1,3,4,7,2,7,15,24),stringsAsFactors = F)

Find differences and appropriate id's.

data.frame(groups = unlist(tapply(x$groups,INDEX = x$groups,FUN = function(x){x[-1]})),
       id = unlist(tapply(x$id,INDEX = x$groups,FUN = function(x){x[-1]})),
       difference = unlist(tapply(x$data,INDEX = x$groups,FUN = diff)))

Your dates might need a different function to calculate the difference. I don't know what format your dates are stored as, and I'm lazy about recreating data.

If you are sure about the uniform format of your data, this will likely perform better:

x$diff <- c(0,diff(x$data))
x[x$id != 'A', ]
ARobertson
  • 2,857
  • 18
  • 24