8
$\begingroup$

I have a dataset with a very simple logfile-like structure, I want to subset the data according to date ranges but can only do on one parameter.

my data looks like this:

            date_time loc_id node  energy   kgco2 
1 2009-02-27 00:11:08     87  103 0.00000 0.00000 
2 2009-02-27 01:05:05     87  103 7.00000 3.75900 
3 2009-02-27 02:05:05     87  103 6.40039 3.43701 
4 2009-02-27 03:05:05     87  103 4.79883 2.57697 
5 2009-02-27 04:05:05     87  103 4.10156 2.20254 
6 2009-02-27 05:05:05     87  103 2.59961 1.39599

the file includes data for a a whole year, I want to create summary plots for every month and perhaps week

I am processing the date_time as follows:

> dt <-as.POSIXlt(ae$date_time)
> ae$dt <- dt
> names(ae$dt)
[1] "sec"   "min"   "hour"  "mday"  "mon"   "year"  "wday"  "yday"  "isdst"

now I'm trying to subset the data as:

> x <- ae$energy[ae$dt$year=="110" & ae$dt$mon=="10"]
> x
numeric(0)

"110" is because the following:

> range(ae$dt$year)
[1] 109 110

I have also tried the following with no luck:

> d <- subset(ae, (dt$year=="110" & dt$mon=="10"), select=energy)

these however do work:

> d <- subset(ae, dt$year=="110", select=energy)

and so does this

> d <- subset(ae, dt$mon=="10", select=energy)

any ideas on how can I subset by selecting both year and month?

Thanks,

$\endgroup$
2
  • 2
    $\begingroup$ Your code doesn't work because the components of a POSIXlt object are numeric/integer, not character. But as Shane said, you really should be using a time-series class... $\endgroup$ Oct 29 '10 at 15:29
  • $\begingroup$ yeah, if you notice I already posted the solution below a few days ago. $\endgroup$
    – G Garcia
    Nov 5 '10 at 17:16
6
$\begingroup$

ok timeseries seem to have done the trick:

aets <- as.xts(read.zoo("n8_energy_actual2009_2010.csv", header=TRUE, sep=",", FUN=as.POSIXct))
eats.2010 <- aets["2010-01::2010-10"]
$\endgroup$
4
$\begingroup$

A few points:

  1. I'm not sure why that's happening. Clearly the POSIXlt slots are wrong. I typically use POSIXct unless I absolutely need to adjust the slots.
  2. One option is to use the dates directly rather than messing with the slots, and say <= and >= to subset. Something like ae[ae$date >= as.POSIXlt("2009-10-01") & ae$date < as.POSIXlt("2009-11-01"),]
  3. You should consider using a time series for this, since that's the exact purpose of that data structure (and they provide many useful functions for dealing with data over time). One of the most common is zoo. xts also includes a number of functions that can help with this kind of thing.
$\endgroup$
2
  • $\begingroup$ Thanks Shane, I have not had any esperience analysing timeseries i tried briefly dxts = as.xts(as.POSIXct(ae$date_time)) but had no luck any pointers? $\endgroup$
    – G Garcia
    Oct 26 '10 at 20:14
  • $\begingroup$ +1 for recommending zoo. It would make your task much easier. xts is meant more for package implementors, though I have used it directly. $\endgroup$
    – Wayne
    Jun 23 '12 at 1:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.