Skip to content
Prev 302421 / 398503 Next

find date between two other dates

Hello,

You can use a function that returns the number you want, not a logical 
value.
But first, it's a bad idea to have a data.frame and a vector with the 
same name, so, in what follows, I've altered the df name.

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")
ddat <- data.frame(ddate=ddate)   # Here, different name.

season.month <- function(x){
     x <- as.integer(format(x, format="%m"))
     ifelse(x == 12L, 1L, x + 1L)
}

season.month(ddate)
ddat$season <- season.month(ddate)
str(ddat)
'data.frame':   6 obs. of  2 variables:
  $ ddate : POSIXct, format: "1998-12-29 20:00:33" "1999-01-02 05:20:44" ...
  $ season: int  1 2 2 3 4 4

ddat
                 ddate 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

Hope this helps,

Rui Barradas

Em 05-08-2012 21:30, penguins escreveu: