同一时间戳的不同ID的值

时间:2014-10-08 12:13:59

标签: r dataframe timestamp

我想以相同的时间间隔创建一个具有不同ID值的数据框,如果该值不存在,我必须携带该特定ID的最后一个值。这是我的数据框

 df
  ID                FROM                  TO    VALUE
1  a 2014-09-24 20:00:52    2014-09-24 20:00:52 121.9000
2  a 2014-09-24 20:02:38    2014-09-24 20:02:39 111.9000
3  a 2014-09-24 20:13:46    2014-09-24 20:13:48 120.9000
4  b 2014-09-24 20:00:52    2014-09-24 20:00:52 113.5631
5  b 2014-09-24 20:02:38    2014-09-24 20:02:38 112.5631
6  b 2014-09-24 20:13:39    2014-09-24 20:13:49 110.5631

从2014-09-24 20:02:38到2014-09-24 20:02:39,a的价值为111.9000。我希望将df的输出作为

a           b               FROM             TO
121.9000    113.5631    2014-09-24 20:00:52  2014-09-24 20:00:52
111.9000    112.5631    2014-09-24 20:02:38  2014-09-24 20:02:38  
111.9000    110.5631    2014-09-24 20:13:39  2014-09-24 20:13:45 # a's value is carry forward
120.9000    110.5631    2014-09-24 20:13:46  2014-09-24 20:13:49 

任何有效的方法来实现上述结果。谢谢。

dput(df)
structure(list(ID = c("a", "a", "a", "b", "b", "b"), FROM = c("2014-09-24 20:00:52", 
"2014-09-24 20:02:38", "2014-09-24 20:13:46", "2014-09-24 20:00:52", 
"2014-09-24 20:02:38", "2014-09-24 20:13:39"), TO = c("2014-09-24 20:00:52", 
"2014-09-24 20:02:39", "2014-09-24 20:13:48", "2014-09-24 20:00:52", 
"2014-09-24 20:02:38", "2014-09-24 20:13:49"), VALUE = c(121.899978637695, 
111.899978637695, 120.899978637695, 113.563064575195, 112.563064575195, 
110.563064575195)), .Names = c("ID", "FROM", "TO", "VALUE"), row.names = c(NA, 
-6L), class = "data.frame")

0 个答案:

没有答案