计算时间差异,按行分组的数据

时间:2013-06-28 00:34:38

标签: r zoo reshape2

我有以下格式的数据集

ID  DATETIME          VALUE  
1   4/2/2012 10:00      300  
1   5/2/2012 23:00      150  
1   6/3/2012 10:00      650  
2   1/2/2012 10:00      450  
2   2/2/2012 13:00      240  
3   6/5/2012 09:00      340  
3   7/5/2012 23:00      240

我想首先计算每个ID的第一个实例到每个后续时间的时差。

ID  DATETIME          VALUE  DIFTIME(days)  
1   4/2/2012 10:00      300   0  
1   5/2/2012 23:00      150   1.3  
1   6/3/2012 10:00      650   33  
2   1/2/2012 10:00      450   0  
2   2/2/2012 13:00      240   1  
3   6/5/2012 09:00      340   0  
3   7/5/2012 23:00      240   1  

然后我想把它变成一种宽格式

ID   0    1    1.3    33  
1    300  na  150  na  650  
2   450  240  na   na   
3   340  240 na na 

1 个答案:

答案 0 :(得分:1)

这是使用data.tablereshape2包的解决方案:

library(data.table)
DT <- as.data.table(dat)
DT[, `:=`(DIFTIME, c(0, diff(as.Date(DATETIME)))), by = "ID"]
##    ID VALUE            DATETIME DIFTIME
## 1:  1   300 2012-02-04 10:00:00       0
## 2:  1   150 2012-02-05 23:00:00       1
## 3:  1   650 2012-03-06 10:00:00      30
## 4:  2   450 2012-02-01 10:00:00       0
## 5:  2   240 2012-02-02 13:00:00       1
## 6:  3   340 2012-05-06 09:00:00       0
## 7:  3   240 2012-05-07 23:00:00       1

library(reshape2)

dcast(formula = ID ~ DIFTIME, data = DT[, list(ID, DIFTIME, VALUE)])
##   ID   0   1  30
## 1  1 300 150 650
## 2  2 450 240  NA
## 3  3 340 240  NA

数据方便

这是我的数据:

structure(list(ID = c(1L, 1L, 1L, 2L, 2L, 3L, 3L), DATETIME = structure(c(1328346000, 
1328479200, 1331024400, 1328086800, 1328184000, 1336287600, 1336424400
), class = c("POSIXct", "POSIXt"), tzone = ""), VALUE = c(300L, 
150L, 650L, 450L, 240L, 340L, 240L)), .Names = c("ID", "DATETIME", 
"VALUE"), class = "data.frame", row.names = c(NA, 7L))