[R] find date between two other dates

arun smartpink111 at yahoo.com
Sun Aug 5 23:14:45 CEST 2012


HI,

Try this:
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") 
ddate1<-data.frame(date=ddate)
date2<-c("01/12/1998 00:00:00", "31/12/1998 23:59:59", "01/01/1999 00:00:00", "31/01/1999 23:59:59", "01/02/1999 00:00:00", "28/02/1999 23:59:59",
"01/03/1999 00:00:00", "31/03/1999 23:59:59")
date3<-as.POSIXct(strptime(date2, "%d/%m/%Y %H:%M:%S"), "GMT")
ddate1[ddate1$date<=date3[2]& ddate1$date>=date3[1],"Season"]<-1
ddate1[ddate1$date<date3[4]& ddate1$date>=date3[3],"Season"]<-2
ddate1[ddate1$date<date3[6]& ddate1$date>=date3[5],"Season"]<-3
ddate1[ddate1$date<date3[8]& ddate1$date>=date3[7],"Season"]<-4

 ddate1
                 date Season
1 1998-12-29 20:00:33      1
2 1999-01-02 05:20:44      2
3 1999-01-02 06:18:36      2
4 1999-02-02 07:06:59      3
5 1999-03-02 07:10:56      4
6 1999-03-02 07:57:18      4


A.K.

----- Original Message -----
From: penguins <catrsw at bas.ac.uk>
To: r-help at r-project.org
Cc: 
Sent: Sunday, August 5, 2012 4:30 PM
Subject: [R] find date between two other dates

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.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list