Hi, how it's possible to extract the year and the number of days from Julian date. i'm little confused about the last two functions and ?years . EDATE comes from sqlQuery with as.is=T EDATE <- as.POSIXlt(datvears$ENROLLDAY) Many thanks, Christian> EDATE[1:5][1] "2000-06-30 11:25:01" "2000-06-30 11:39:55" "2000-06-30 12:11:11" [4] "2000-06-30 12:13:32" "2000-06-30 12:50:12"> weekdays(EDATE[1:5])[1] "Freitag" "Freitag" "Freitag" "Freitag" "Freitag"> months(EDATE[1:5])[1] "Juni" "Juni" "Juni" "Juni" "Juni"> years(EDATE[1:5])NULL> days(EDATE[1:5])NULL>
days and years are from the chron package and operate on chron objects, not POSIXlt objects. If you have a POSIXlt object, EDATE, you can get the years and julian day of the year like this: as.numeric(format(EDATE,"%Y")) as.numeric(format(EDATE,"%j")) See ?strptime for those % codes and many others. Alternately you can convert your dates to chron objects instead of POSIXlt objects and then you can use years and days as you did below. See the chron package for that. --- Date: Mon, 8 Mar 2004 15:50:01 +0100 From: Christian Schulz <ozric at web.de> To: <r-help at stat.math.ethz.ch> Subject: [R] years from as.POSIXlt Hi, how it's possible to extract the year and the number of days from Julian date. i'm little confused about the last two functions and ?years . EDATE comes from sqlQuery with as.is=T EDATE <- as.POSIXlt(datvears$ENROLLDAY) Many thanks, Christian> EDATE[1:5][1] "2000-06-30 11:25:01" "2000-06-30 11:39:55" "2000-06-30 12:11:11" [4] "2000-06-30 12:13:32" "2000-06-30 12:50:12"> weekdays(EDATE[1:5])[1] "Freitag" "Freitag" "Freitag" "Freitag" "Freitag"> months(EDATE[1:5])[1] "Juni" "Juni" "Juni" "Juni" "Juni"> years(EDATE[1:5])NULL> days(EDATE[1:5])NULL>
On Mon, 8 Mar 2004, Christian Schulz wrote:> how it's possible to extract the year and the number > of days from Julian date. i'm little confused about the last two > functions and ?years . > > EDATE comes from sqlQuery with as.is=T > EDATE <- as.POSIXlt(datvears$ENROLLDAY) > > EDATE[1:5] > [1] "2000-06-30 11:25:01" "2000-06-30 11:39:55" "2000-06-30 12:11:11" > [4] "2000-06-30 12:13:32" "2000-06-30 12:50:12" > > weekdays(EDATE[1:5]) > [1] "Freitag" "Freitag" "Freitag" "Freitag" "Freitag" > > months(EDATE[1:5]) > [1] "Juni" "Juni" "Juni" "Juni" "Juni" > > years(EDATE[1:5]) > NULL > > days(EDATE[1:5]) > NULLyears() and days() are not part of base R: from chron, perhaps? See ?julian, which says Note: Other components such as the day of the month or the year are very easy to computes: just use 'as.POSIXlt' and extract the relevant component. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
>as.numeric(format(x, f="%j"))which is the right code, works perfectly, too. Thomas P.
If you search the mail archives for fromchar there are a number of discussions of similar bugs that all seem to come from German or other European users, suggesting locale problems. I suspect it won't be easy for others to reproduce this so you might try to see what you can track down yourself. fromchar is in as.POSIXlt so perhaps you could try to get the bug to appear by using as.POSIXlt directly and then debugging it using debug(as.POSIXlt) and as soon as it defines fromchar debug(fromchar) In terms of an interim solution, you could use chron. For example, for the .leap.seconds problem: require(chron) .leap.seconds.chron <- chron( format(.leap.seconds,"%m/%d/%Y"), format(.leap.seconds,"%H:%M:%S") ) as.numeric(.leap.seconds.chron) where the last line uses the fact that chron stores its dates internally as the number of days since January 1, 1970. --- Date: Wed, 10 Mar 2004 00:04:30 +0100 From: Thomas Petzoldt <thpe at hhbio.wasser.tu-dresden.de> To: <r-help at stat.math.ethz.ch> Subject: Re: [R] years from as.POSIXlt Prof Brian Ripley wrote:> See ?julian, which says > > Note: > > Other components such as the day of the month or the year are very > easy to computes: just use 'as.POSIXlt' and extract the relevant > component. >Hello, unfortunately not all mentioned functions work on all machines. Where> months(.leap.seconds)works on all systems, a call to> julian(leap.seconds)workes perfectly only on Linux (R 1.8.0) but failed on two different XP Machines (German XP version): Error in fromchar(x) : character string is not in a standard unambiguous format I've tested several things, several versions of msvcrt.dll, different PATH settings, locale set to German or USA, R 1.8.1 and R 1.7.1, but the problem remains. On the other hand>as.numeric(format(x, f="%d"))works perfectly, so I do not understand what is wrong with julian() Thomas P.
On Wed, 10 Mar 2004, Thomas Petzoldt wrote:> >as.numeric(format(x, f="%j")) > > which is the right code, works perfectly, too.but the recommended procedure is on the help page ?julian, and of course works perfectly. Just use 1+x$yday if you want 1-based day of the year. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595