[R] as.Date() add a day to a date

David Winsemius dwinsemius at comcast.net
Mon Sep 13 18:08:16 CEST 2010


On Sep 13, 2010, at 11:56 AM, Andrew Yee wrote:

> I'm trying to understand why as.Date() is converting a the modified  
> date of
> a file from August 22 to August 23.
>
>> foo <- file.info(file.to.process)
>> str(foo)
> 'data.frame':   1 obs. of  10 variables:
> $ size  : num 5.37e+09
> $ isdir : logi FALSE
> $ mode  :Class 'octmode'  int 436
> $ mtime : POSIXct, format: "2010-08-22 23:14:52"
> $ ctime : POSIXct, format: "2010-09-13 11:34:31"
> $ atime : POSIXct, format: "2010-09-09 15:42:00"
> $ uid   : int 503
> $ gid   : int 503
> $ uname : chr "ayee"
> $ grname: chr "ayee"
>
>> as.Date(foo$mtime)
> [1] "2010-08-23"
>
> In this case as.Date(foo$mtime) gives August 23.  I would have  
> expected
> August 22.

You are pretty close to midnight. Probably a TZ issue. Check your  
locale settings versus UTC/GMT.

 >  dt <- as.POSIXct("2010-08-22 23:14:52")
 > as.Date(dt)
[1] "2010-08-23"

Happens to me too, but when its 23:14 here, (US-EDT) it's the next day  
in Greenwich. The help page says:
"The last [POSIXct] is converted to days by ignoring the time after  
midnight in the representation of the time in UTC." That is not  
entirely clear to my reading, but I am forced to conclude that it  
means as.Date returns the Date in UTC terms rather than in local terms.

-- 
David.

>
> Thanks,
> Andrew
>
>> sessionInfo()
> R version 2.11.1 Patched (2010-09-11 r52901)
> Platform: x86_64-unknown-linux-gnu (64-bit)
>
> locale:
> [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
> [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
> [5] LC_MONETARY=C              LC_MESSAGES=en_US.UTF-8
> [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
> [9] LC_ADDRESS=C               LC_TELEPHONE=C
> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> 	[[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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list