Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select date range with POSIXct type

Tags:

date

r

lubridate

I'm working on a project where I want to track birth totals nine months after another event (wondering if there is a correlation). It was suggested I use the "lubridate" package to combine MONTH and YEAR in my data frames (which I was seeking to do).

I have successfully used lubridate in both data frames. The problem is that I only want to select a certain date range from one of those data frames, and I'm having difficulty doing this. I think it's because the type of data is POSIXct:

str(model.weather)
'data.frame':   467 obs. of  2 variables:
 $ DATE      : POSIXct, format: "2006-01-01" "2006-01-01" "2006-01-01" "2006-01-01" ...
 $ EVENT_TYPE: Factor w/ 8 levels "Hail","Heavy Snow",..: 2 2 2 2 2 2 3 2 3 3 ...

I have tried converting to numeric, but unfortunately that does horrible, horrible things to the data in my DATE variable. I've tried converting to factor, character, and integer, too - none of them seem to work.

I'm trying to use the "subset" function to select the necessary date range:

model.weather <- subset(model.weather, DATE >= 2006-04-01 | DATE <= 2011-03-01)

Unfortunately, this just returns the original data, without doing any filtering.

Can anyone help?

like image 275
thebonafortuna Avatar asked Oct 26 '25 09:10

thebonafortuna


1 Answers

If DATE contains only dates (without hours and etc, like in your example data above) you can convert it into as.Date class and then operate on it

model.weather$DATE <- as.Date(model.weather$DATE)

model.weather <- subset(model.weather, DATE >= "2006-04-01" & DATE <= "2011-03-01")

or (a better solution)

model.weather <- model.weather[model.weather$DATE >= "2006-04-01" & model.weather$DATE <= "2011-03-01", ]
like image 150
David Arenburg Avatar answered Oct 27 '25 21:10

David Arenburg