[R] Strange behaviour of as.Date function

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Sep 23 16:00:12 CEST 2005


datai[1,1] appears to be a 'POSIXct' object.  Which date that is depends 
on the locale, and as.Date uses UTC (see ?as.Date).  For me:

> d <- as.POSIXct("2005-07-01")
> d
[1] "2005-07-01 BST"
> format(d, tz="GMT")
[1] "2005-06-30 23:00:00"
> as.Date(d)
[1] "2005-06-30"

Use

as.Date(d + 23.99*3600)

to avoid this.


On Fri, 23 Sep 2005, Vittorio wrote:

> Dear All,
> I'm happily extracting data of temperature from an oracle db
> under R via RODBC. After manipulating the extracted data I put them
> into a data.frame 'dati' which is as follows:
>
>> dati
>         DATA tm.
> UDINE/RIVOLTO tm.TORINO/CASELLE
> 1  2005-07-01
> 22.35             23.80
> 2  2005-07-02            22.70
> 22.85
> 3  2005-07-03            23.80             24.30
> 4  2005-07-
> 04            23.80             25.40
> ..........
>
> and
>
>> str(dati)
> `data.frame':	11 obs. of  3 variables:
> $ DATA             :'POSIXct',
> format: chr  "2005-07-01" "2005-07-02" "2005-07-03" "2005-07-04" ...
> $
> tm.UDINE/RIVOLTO : num  22.4 22.7 23.8 23.8 21.8 ...
> $ tm.
> TORINO/CASELLE: num  23.8 22.9 24.3 25.4 21.8 ...
> - attr(*,
> "reshapeWide")=List of 5
>  ..$ v.names: NULL
>  ..$ timevar: chr "NOME"
>  ..$ idvar  : chr "DATA"
>  ..$ times  : Factor w/ 2 levels
> "TORINO/CASELLE",..: 2 1
>  ..$ varying: chr [1, 1:2] "tm.UDINE/RIVOLTO"
> "tm.TORINO/CASELLE"
>>
>
> You see that the first field DATA is POSIXct
>
> Now
>> dati[1,1]
> [1] "2005-07-01 ora solare Europa occidentale"
>
> BUT
>
>>
> as.Date(dati[1,1],"%d%m%Y")
> [1] "2005-06-30"
>
> How come?
> What is wrong
> with it (or better with me)?
>
> Ciao
> Vittorio
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

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




More information about the R-help mailing list