Hi, I'm a bit confused how julian() works. If I understand right, it returns the number of days since the origin. I have a vector:> SLDATX[1:10][1] "1986-01-06" "1986-01-17" "1986-02-02" "1986-02-04" [5] "1986-02-04" "1986-02-21" "1986-03-06" "1986-03-25" [9] "1986-04-06" "1986-04-10" And when I did:> TIMESOLD <- as.numeric(julian(as.POSIXlt(SLDATX),+ origin = as.POSIXct("1986-01-01", ""))) I got:> TIMESOLD[1:10][1] 5.00000 16.00000 32.00000 34.00000 34.00000 51.00000 [7] 64.04167 83.04167 95.04167 99.04167 THe first 6 values from TIMESOLD is obvious, however I'm not sure why I got decimals from the 7th value, as my input vector does not have any specific "times" after the dates. Any insights would be greatly appreciated...;-D -- Cheers, Kevin --------------------------------------------------------------- "Try not. Do, do! Or do not. There is no try" Jedi Master Yoda ---- Ko-Kang Kevin Wang, MSc SLC STATS 10x Workshop Coordinator University of Auckland New Zealand Homepage: stat.auckland.ac.nz/~kwan022 Ph: 373-7599 x88475
Ko-Kang Kevin Wang wrote:> > Hi, > > I'm a bit confused how julian() works. If I understand right, it returns > the number of days since the origin. > > I have a vector: > > SLDATX[1:10] > [1] "1986-01-06" "1986-01-17" "1986-02-02" "1986-02-04" > [5] "1986-02-04" "1986-02-21" "1986-03-06" "1986-03-25" > [9] "1986-04-06" "1986-04-10" > > And when I did: > > TIMESOLD <- as.numeric(julian(as.POSIXlt(SLDATX), > + origin = as.POSIXct("1986-01-01", ""))) > > I got: > > TIMESOLD[1:10] > [1] 5.00000 16.00000 32.00000 34.00000 34.00000 51.00000 > [7] 64.04167 83.04167 95.04167 99.04167 > > THe first 6 values from TIMESOLD is obvious, however I'm not sure why I > got decimals from the 7th value, as my input vector does not have any > specific "times" after the dates.That's because of the timezone you are using (winter/summertime). Look what the difference of 0.04167 really is: 0.04167 * 24 ~ 1 hour..... Uwe Ligges> Any insights would be greatly appreciated...;-D > > -- > Cheers, > > Kevin > > --------------------------------------------------------------- > "Try not. Do, do! Or do not. There is no try" > Jedi Master Yoda > > ---- > Ko-Kang Kevin Wang, MSc > SLC STATS 10x Workshop Coordinator > University of Auckland > New Zealand > Homepage: stat.auckland.ac.nz/~kwan022 > Ph: 373-7599 x88475 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > stat.math.ethz.ch/mailman/listinfo/r-help
What you can do to handle this timezone problem is either to use POSIXt with GMT or use chron (which does not use timezones so can't cause problems like this): Suppose: SLDATX <- c( "1986-01-06", "1986-01-17", "1986-02-02", "1986-02-04", ,"1986-02-04", "1986-02-21", "1986-03-06", "1986-03-25", ,"1986-04-06", "1986-04-10" ) # then using POSIXt in the GMT timezone: TIMESOLD <- as.numeric( julian( as.POSIXlt( SLDATX, tz="GMT" ), origin = as.POSIXct( "1986-01-01", tz = "GMT" ) ) ) # or the alternative using chron: require(chron) TIMESOLD2 <- as.numeric( chron( SLDATX, format="y-m-d", origin = c( month = 1, day = 1, year = 1986 ) ) ) all.equal(TIMESOLD,TIMESOLD2) --- Date: Mon, 15 Dec 2003 22:37:59 +0100 From: Uwe Ligges <ligges at statistik.uni-dortmund.de> To: Ko-Kang Kevin Wang <kwan022 at stat.auckland.ac.nz> Cc: R Help <r-help at stat.math.ethz.ch> Subject: Re: [R] Julian Dates Ko-Kang Kevin Wang wrote:> > Hi, > > I'm a bit confused how julian() works. If I understand right, it returns > the number of days since the origin. > > I have a vector: > > SLDATX[1:10] > [1] "1986-01-06" "1986-01-17" "1986-02-02" "1986-02-04" > [5] "1986-02-04" "1986-02-21" "1986-03-06" "1986-03-25" > [9] "1986-04-06" "1986-04-10" > > And when I did: > > TIMESOLD <- as.numeric(julian(as.POSIXlt(SLDATX), > + origin = as.POSIXct("1986-01-01", ""))) > > I got: > > TIMESOLD[1:10] > [1] 5.00000 16.00000 32.00000 34.00000 34.00000 51.00000 > [7] 64.04167 83.04167 95.04167 99.04167 > > THe first 6 values from TIMESOLD is obvious, however I'm not sure why I > got decimals from the 7th value, as my input vector does not have any > specific "times" after the dates.That's because of the timezone you are using (winter/summertime). Look what the difference of 0.04167 really is: 0.04167 * 24 ~ 1 hour..... Uwe Ligges> Any insights would be greatly appreciated...;-D > > -- > Cheers, > > Kevin > > --------------------------------------------------------------- > "Try not. Do, do! Or do not. There is no try" > Jedi Master Yoda > > ---- > Ko-Kang Kevin Wang, MSc > SLC STATS 10x Workshop Coordinator > University of Auckland > New Zealand > Homepage: stat.auckland.ac.nz/~kwan022 > Ph: 373-7599 x88475 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > stat.math.ethz.ch/mailman/listinfo/r-help
Assuming you want to find a whole number months and quarters: require(chron) z <- chron( SLDATX, format="y-m-d" ) months <- with( month.day.year(z), 12*(year-1986)+month-1 ) quarters <- months %/% 3 --- Date: Tue, 16 Dec 2003 13:58:13 +1300 (NZDT) From: Ko-Kang Kevin Wang <kwan022 at stat.auckland.ac.nz> To: Gabor Grothendieck <ggrothendieck at myway.com> Cc: <ligges at statistik.uni-dortmund.de>, <r-help at stat.math.ethz.ch> Subject: Re: [R] Julian Dates Thanks! chron() is very useful indeed. Just out of interest, is it possible to do, say in this case, the number of months (or quarters) after January 1986? i.e. use a different time interval? On Mon, 15 Dec 2003, Gabor Grothendieck wrote:> What you can do to handle this timezone problem is either to use > POSIXt with GMT or use chron (which does not use timezones so > can't cause problems like this): > > Suppose: > > SLDATX <- c( "1986-01-06", "1986-01-17", "1986-02-02", "1986-02-04", > ,"1986-02-04", "1986-02-21", "1986-03-06", "1986-03-25", > ,"1986-04-06", "1986-04-10" ) > > # then using POSIXt in the GMT timezone: > > TIMESOLD <- as.numeric( julian( as.POSIXlt( SLDATX, tz="GMT" ), > origin = as.POSIXct( "1986-01-01", tz = "GMT" ) ) ) > > # or the alternative using chron: > > require(chron) > TIMESOLD2 <- as.numeric( chron( SLDATX, format="y-m-d", > origin = c( month = 1, day = 1, year = 1986 ) ) ) > > all.equal(TIMESOLD,TIMESOLD2) >-- Cheers, Kevin --------------------------------------------------------------- "Try not. Do, do! Or do not. There is no try" Jedi Master Yoda