[R] find date between two other dates

penguins catrsw at bas.ac.uk
Sun Aug 5 22:30:38 CEST 2012


Hi,

I am trying to assign "Season" values to dates depending on when they occur.

For example, the following dates would be assigned the following "Season"
numbers based on the "season" intervals detailed below in the code:

ddate                               Season
29/12/1998 20:00:33       1
02/01/1999 05:20:44       2
02/01/1999 06:18:36       2
02/02/1999 07:06:59       3
02/03/1999 07:10:56       4
02/03/1999 07:57:18       4

My approach so far doesnt work because of the time stamps and is probably
very long winded. However, to prevent errors I would prefer to keep the date
formats as dd/mm/yyyy as oppose to a numeric format. Any help on the
following code would be gratefully recieved:
 
ddate <-  c("29/12/1998 20:00:33", "02/01/1999 05:20:44", "02/01/1999
06:18:36", "02/02/1999 07:06:59", "02/03/1999 07:10:56", "02/03/1999
07:57:18")
ddate <- as.POSIXct(strptime(ddate, "%d/%m/%Y %H:%M:%S"), "GMT")

is.between<-function(x, a, b) {
       (x > a) & (b > x)
}

ddate$s1 <- is.between(ddate, 01/12/1998 00:00:00, 31/12/1998 23:59:59)
ddate$s2 <- is.between(ddate, 01/01/1999 00:00:00, 31/01/1999 23:59:59)
ddate$s3 <- is.between(ddate, 01/02/1999 00:00:00, 28/02/1999 23:59:59)
ddate$s4 <- is.between(ddate, 01/03/1999 00:00:00, 31/03/1999 23:59:59)

Many thanks



--
View this message in context: http://r.789695.n4.nabble.com/find-date-between-two-other-dates-tp4639231.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list