Solved – Subsetting a data-frame in R based on dates

rtime series

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,

Best Answer

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"]
Related Question