Glenn Biren
Glenn Biren

Reputation: 63

How can I filter specifically for certain months if the days are not the same in each year?

This is probably a very simple question that has been asked already but.. I have a data frame that I have constructed from a CSV file generated in excel. The observations are not homogeneously sampled, i.e they are for "On Peak" times of electricity usage. That means they exclude different days each year. I have 20 years of data (1993-2012) and am running both non Robust and Robust LOESS to extract seasonal and linear trends. After the decomposition has been done, I want to focus only on the observations from June through September.

How can I create a new data frame of just those results?

Sorry about the formatting, too.

         Date MaxLoad     TMAX
1  1993-01-02    2321 118.6667
2  1993-01-04    2692 148.0000
3  1993-01-05    2539 176.0000
4  1993-01-06    2545 172.3333
5  1993-01-07    2517 177.6667
6  1993-01-08    2438 157.3333
7  1993-01-09    2302 152.0000
8  1993-01-11    2553 144.3333
9  1993-01-12    2666 146.3333
10 1993-01-13    2472 177.6667

Upvotes: 6

Views: 12672

Answers (1)

Josh O'Brien
Josh O'Brien

Reputation: 162321

As Joran notes, you don't need anything other than base R:

## Reproducible data
df <-
data.frame(Date = seq(as.Date("2009-03-15"), as.Date("2011-03-15"), by="month"),
           MaxLoad = floor(runif(25,2000,3000)), TMAX=runif(25,100,200))

## One  option
df[months(df$Date) %in% month.name[6:9],]
#          Date MaxLoad     TMAX
# 4  2009-06-15    2160 188.4607
# 5  2009-07-15    2151 164.3946
# 6  2009-08-15    2694 110.4399
# 7  2009-09-15    2460 150.4076
# 16 2010-06-15    2638 178.8341
# 17 2010-07-15    2246 131.3283
# 18 2010-08-15    2483 112.2635
# 19 2010-09-15    2174 160.9724

## Another option: strftime() will be more _generally_ useful than months()
df[as.numeric(strftime(df$Date, "%m")) %in% 6:9,]

Upvotes: 10

Related Questions