删除具有顺序相似数据的行 - R Zoo

时间:2012-07-20 11:18:51

标签: r xts zoo

我有一个按时间编入索引的动物园数据集。有些行有相同的数据,所以我基本上只想删除这些重复的行。

                       redpardiff        relpar
2012-07-05 10:19:38 -9.531491e-05  4.727280e-07
2012-07-05 10:19:41 -9.531491e-05  4.727280e-07
2012-07-05 10:19:47 -9.531491e-05  4.727280e-07
2012-07-05 10:19:47 -9.531491e-05 -9.999995e-01
2012-07-05 10:19:47 -9.531491e-05  1.000000e+00
2012-07-05 10:19:49 -9.531491e-05 -9.999995e-01

删除后应该看起来像

                       redpardiff        relpar
2012-07-05 10:19:38 -9.531491e-05  4.727280e-07
2012-07-05 10:19:47 -9.531491e-05 -9.999995e-01
2012-07-05 10:19:47 -9.531491e-05  1.000000e+00
2012-07-05 10:19:49 -9.531491e-05 -9.999995e-01

进行顺序比较非常缓慢。 有没有更好的方法进行这样的练习?

structure(c(-9.53149088309146e-05, -9.53149088309146e-05, -9.53149088309146e-05, 
-9.53149088309146e-05, -9.53149088309146e-05, -9.53149088309146e-05, 
4.72727990086241e-07, 4.72727990086241e-07, 4.72727990086241e-07, 
-0.99999952727201, 1.00000047272799, -0.99999952727201), .Dim = c(6L, 
2L), .Dimnames = list(NULL, c("redpardiff", "relpar")), index = structure(c(1341463778.55163, 
1341463781.40801, 1341463787.2642, 1341463787.52668, 1341463787.78777, 
1341463789.36693), class = c("POSIXct", "POSIXt")), class = "zoo")

编辑:“显然准时”

1 个答案:

答案 0 :(得分:3)

 x[c(1,which(rowSums(abs(diff(x)))!=0)+1),]
                       redpardiff        relpar
2012-07-05 05:49:38 -9.531491e-05  4.727280e-07
2012-07-05 05:49:47 -9.531491e-05 -9.999995e-01
2012-07-05 05:49:47 -9.531491e-05  1.000000e+00
2012-07-05 05:49:49 -9.531491e-05 -9.999995e-01