Subset a data frame based on date
Utility function to make it easier to select periods from a data frame before sending to a function
selectByDate( mydata, start = "1/1/2008", end = "31/12/2008", year = 2008, month = 1, day = "weekday", hour = 1 )
mydata |
A data frame containing a |
start |
A start date string in the form d/m/yyyy e.g. “1/2/1999” or in ‘R’ format i.e. “YYYY-mm-dd”, “1999-02-01” |
end |
See |
year |
A year or years to select e.g. |
month |
A month or months to select. Can either be numeric e.g.
|
day |
A day name or or days to select. |
hour |
An hour or hours to select from 0-23 e.g. |
This function makes it much easier to select periods of interest from a data frame based on dates in a British format. Selecting date/times in R format can be intimidating for new users. This function can be used to select quite complex dates simply - see examples below.
Dates are assumed to be inclusive, so start = "1/1/1999"
means that
times are selected from hour zero. Similarly, end = "31/12/1999"
will
include all hours of the 31st December. start
and end
can also
be in standard R format as a string i.e. "YYYY-mm-dd", so start =
"1999-01-01"
is fine.
All options are applied in turn making it possible to select quite complex dates
David Carslaw
## select all of 1999 data.1999 <- selectByDate(mydata, start = "1/1/1999", end = "31/12/1999") head(data.1999) tail(data.1999) # or... data.1999 <- selectByDate(mydata, start = "1999-01-01", end = "1999-12-31") # easier way data.1999 <- selectByDate(mydata, year = 1999) # more complex use: select weekdays between the hours of 7 am to 7 pm sub.data <- selectByDate(mydata, day = "weekday", hour = 7:19) # select weekends between the hours of 7 am to 7 pm in winter (Dec, Jan, Feb) sub.data <- selectByDate(mydata, day = "weekend", hour = 7:19, month = c("dec", "jan", "feb"))
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.