[R] find date between two other dates

arun smartpink111 at yahoo.com
Mon Aug 6 14:40:13 CEST 2012


Hi,

I run the second list of codes (is.between()) again from the sent mail.  It works fine for me.  I am using R 2.15 on Ubuntu 12.04.    

sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=C                 LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] stringr_0.6   reshape_0.8.4 plyr_1.7.1   

####################################
is.between<-function(x,a,b){
  x<a& x>=b
  }
 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[is.between(ddate1$date,date3[2],date3[1]),"Season"]<-1 
  ddate1[is.between(ddate1$date,date3[4],date3[3]),"Season"]<-2
  ddate1[is.between(ddate1$date,date3[6],date3[5]),"Season"]<-3
  ddate1[is.between(ddate1$date,date3[8],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
#####################################
Not sure how you are getting NA.  One possibility is that if you used "date2"(which is not converted) instead of "date3" (as in date3 <-as.POSIXct....)
If you did this:

    ddate1[is.between(ddate1$date,date2[2],date2[1]),"Season"]<-1
    ddate1[is.between(ddate1$date,date2[4],date2[3]),"Season"]<-2
    ddate1[is.between(ddate1$date,date2[6],date2[5]),"Season"]<-3
    ddate1[is.between(ddate1$date,date2[8],date2[7]),"Season"]<-4 
    ddate1
                 date Season
1 1998-12-29 20:00:33     NA
2 1999-01-02 05:20:44     NA
3 1999-01-02 06:18:36     NA
4 1999-02-02 07:06:59     NA
5 1999-03-02 07:10:56     NA
6 1999-03-02 07:57:18     NA
A.K.





----- Original Message -----
From: penguins <catrsw at bas.ac.uk>
To: r-help at r-project.org
Cc: 
Sent: Monday, August 6, 2012 4:13 AM
Subject: Re: [R] find date between two other dates

Thanks arun and Rui; 3 fantastic suggestions. 

The Season interval is not always a month so arun's suggestion works better
for this dataset. I couldn't get the as.between function to work on arun's
second suggestion, it only returned NAs. 

However, arun's first suggestion worked a treat!

Many thanks 



--
View this message in context: http://r.789695.n4.nabble.com/find-date-between-two-other-dates-tp4639231p4639253.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