# Subsetting a data-frame in R based on dates [closed]

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,

• 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... Oct 29 '10 at 15:29
• yeah, if you notice I already posted the solution below a few days ago. Nov 5 '10 at 17:16

## 2 Answers

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


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.
• 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? Oct 26 '10 at 20:14
• +1 for recommending zoo. It would make your task much easier. xts is meant more for package implementors, though I have used it directly. Jun 23 '12 at 1:41