我有一些带有POSIXct列的大型数据框。
time longitude latitude
1 2012-10-28 23:01:00 16.42125 52.37832
2 2012-10-28 23:02:00 16.42125 52.37832
3 2012-10-28 23:03:00 16.42127 52.37832
...
出于兼容性原因,我在某些地方存储时间值而不是行索引。如何获得具有这些时间值的(总是连续的)行?
有了索引,我会这样做:
indices <- 1:3
rows <- dat[indices,]
但随着时间的推移:
times <- dat$time[1:3]
rows <- ???
什么是有效的解决方案?
答案 0 :(得分:4)
切换到包xts。这里有一个随机的例子:
R> dat <- xts(as.matrix(data.frame(long=rnorm(20, 16, 0.1),
+ lat=rnorm(20, 52, 0.1))),
+ order.by=ISOdatetime(2012,10,28,23,00,00)+seq(1,20))
R> dat
long lat
2012-10-28 23:00:01 16.1100 52.0176
2012-10-28 23:00:02 15.7947 52.1227
2012-10-28 23:00:03 15.9632 52.1097
2012-10-28 23:00:04 16.1478 51.8562
2012-10-28 23:00:05 16.0800 52.0969
2012-10-28 23:00:06 15.9444 51.9217
2012-10-28 23:00:07 16.1291 52.1710
2012-10-28 23:00:08 16.1400 52.0243
2012-10-28 23:00:09 15.8718 52.0717
2012-10-28 23:00:10 16.0093 51.9953
2012-10-28 23:00:11 15.9821 52.1891
2012-10-28 23:00:12 16.0326 51.9598
2012-10-28 23:00:13 15.9243 51.9634
2012-10-28 23:00:14 15.9498 52.1015
2012-10-28 23:00:15 16.1454 51.9398
2012-10-28 23:00:16 15.9191 51.8936
2012-10-28 23:00:17 16.0541 51.7999
2012-10-28 23:00:18 15.7676 52.0641
2012-10-28 23:00:19 16.1154 52.0178
2012-10-28 23:00:20 16.1094 51.9573
R>
您可以轻松地从现有对象创建xts
个对象,从文件中读取等等。此处也有很多示例。
现在你可以使用漂亮的子集:
R> dat["2012-10-28 23:00:10/2012-10-28 23:00:14"]
long lat
2012-10-28 23:00:10 16.0093 51.9953
2012-10-28 23:00:11 15.9821 52.1891
2012-10-28 23:00:12 16.0326 51.9598
2012-10-28 23:00:13 15.9243 51.9634
2012-10-28 23:00:14 15.9498 52.1015
R>
甚至是速记:
R> dat["T23:00:10/T23:00:14"]
long lat
2012-10-28 23:00:10 16.0093 51.9953
2012-10-28 23:00:11 15.9821 52.1891
2012-10-28 23:00:12 16.0326 51.9598
2012-10-28 23:00:13 15.9243 51.9634
2012-10-28 23:00:14 15.9498 52.1015
R>