Andreas
Andreas

Reputation: 7550

How to subset a data.frame by value?

I've got some large data.frames with POSIXct columns.

                 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
...

For compatibility reasons, I store the time values instead of the row indices in some places. How can I obtain the (always consecutive) rows with those time values?

With indices, I'd do this:

indices <- 1:3
rows <- dat[indices,]

But with time:

times <- dat$time[1:3]
rows <- ???

What's an efficient solution?

Upvotes: 2

Views: 183

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368201

Switch to package xts. Here us a random example for you:

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>

You can easily create xts objects from existing ones, read from file, etc pp. Lots of examples here too.

Now you can use the nice subsetting:

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> 

or even in shorthand:

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> 

Upvotes: 4

Related Questions