[R] How to get the day of the year

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Oct 1 14:52:09 CEST 2008


On Wed, 2008-10-01 at 08:42 +0000, Solene Goy wrote:
> Hello,
> I am new to R and I would like to get the day of the year from
> numerous data in the following form:  %a %d/%b/%Y %H:%M:%S (for
> example from Tu 10/Jul/2007 21:59:13 I would like to get 191)
>  
> Whatever I try, I get NAs.

Tu isn't a valid abbreviated for for the day (in my locale at least),
which might explain the NAs and you don't show how you tried to get the
julien day.

Here are two ways that work.

> tmp <- as.Date("Tue 10/Jul/2007 21:59:13", format = "%a %d/%b/%Y %H:%M:%S")
> tmp
[1] "2007-07-10"
> format(tmp, "%j")
[1] "191"
> as.numeric(format(tmp, "%j"))
[1] 191
> tmp <- as.POSIXlt("Tue 10/Jul/2007 21:59:13", format = "%a %d/%b/%Y %H:%M:%S") 
> tmp
[1] "2007-07-10 21:59:13"
> format(tmp, format = "%j")
[1] "191"
> as.numeric(format(tmp, format = "%j"))
[1] 191

Is this what you wanted?

G

>  
> Well... I need your help, please!
> Thanks in advance
> Yours sincerely,
> Solne Goy
> _________________________________________________________________
> 
> [[elided Hotmail spam]]
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20081001/826f94a4/attachment.bin>


More information about the R-help mailing list