Skip to content
Prev 254694 / 398500 Next

subset and as.POSIXct / as.POSIXlt oddness

On 03/24/2011 06:29 AM, Michael Bach wrote:
It does work. Try it.
You have given three arguments to subset.  Your third argument is a poor 
choice for selecting columns. Try:

subset(dfx, dx>  as.POSIXct("2007-06-01 16:00:00")&  14>  as.POSIXlt(dx)$hour
&  as.POSIXlt(dx)$hour<  10)

or better yet,

tmp<- as.POSIXlt( dfx$dx )

subset(dfx, dx>  as.POSIXct("2007-06-01 16:00:00")&  14>  tmp$hour&  tmp$hour<  10)

since the as.POSIXlt is a rather heavyweight operation.
This is not aggregation.  This is selection. It is only when you summarize 
the selected data that you are aggregating.

Normally, the term aggregating is applied when you use a grouping column and 
collapse many values with the same characteristics into one value per set of 
characteristics.  For example using base functions,

dfx$interval <- cut(tmp$hour,c(-1,10,14,24))
aggregate(dfx$dx,list(Interval=dfx$interval),length)

or

aggregate(dfx$dx,list(Hour=tmp$hour),length)

but I find that the plyr library is much more user-friendly than aggregate.