Skip to content

Strange behaviour of as.Date function

3 messages · v.demart@libero.it, Gabor Grothendieck, Brian Ripley

#
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:
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
`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
[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
#
When it converts the time to Date it does it relative to the
GMT time zone, not your time zone.  When its July 1st
in your time zone it can be June 30th in the GMT time zone.

See the article in R News 4/1 Help Desk, and the table at the
end of the article in particular, on ways to handle this.
On 9/23/05, Vittorio <vdemart1 at tin.it> wrote:
#
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:
[1] "2005-07-01 BST"
[1] "2005-06-30 23:00:00"
[1] "2005-06-30"

Use

as.Date(d + 23.99*3600)

to avoid this.
On Fri, 23 Sep 2005, Vittorio wrote: