[R] subset and as.POSIXct / as.POSIXlt oddness

Hadley Wickham hadley at rice.edu
Thu Mar 24 16:18:35 CET 2011


On Thu, Mar 24, 2011 at 8:29 AM, Michael Bach <phaebz at gmail.com> wrote:
> Dear R users,
>
> Given this data:
>
> x <- seq(1,100,1)
> dx <- as.POSIXct(x*900, origin="2007-06-01 00:00:00")
> dfx <- data.frame(dx)
>
> Now to play around for example:
>
> subset(dfx, dx > as.POSIXct("2007-06-01 16:00:00"))
>
> Ok. Now for some reason I want to extract the datapoints between hours
> 10:00:00 and 14:00:00, so I thought well:
>
> subset(dfx, dx > as.POSIXct("2007-06-01 16:00:00"), 14 > as.POSIXlt(dx)$hour
> & as.POSIXlt(dx)$hour < 10)
> Error in as.POSIXlt.numeric(dx) : 'origin' must be supplied

As others have noted you used a , instead of &.  I wanted to point out
that this is a little easier to express with the lubridate package:

subset(dfx, dx > ymd("2007-06-01") & hour(dx) > 14 & hour(x) < 10)

but I presume you meant:

subset(dfx, dx > ymd("2007-06-01") & hour(dx) > 10 & hour(x) < 14)

Hadley


-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



More information about the R-help mailing list