[R] Question on creating Date variable

David Winsemius dwinsemius at comcast.net
Mon Dec 31 19:32:50 CET 2012


On Dec 31, 2012, at 9:12 AM, Christofer Bogaso wrote:

> Hello all,
>
> Let say I have following (numeric) vector:
>
> > x
> [1] 11.00 11.25 11.35 12.01 11.14 13.00 13.25 13.35 14.01 13.14  
> 14.50 14.75 14.85 15.51 14.64
>
> Now, I want to create a 'Date' variable (i.e. I should be able to do  
> all calculations pertaining to date/time and also time-series  
> plotting etc.) like
>
> 2012-12-31 11:00:00 AM, 2012-12-31 11:25:00 AM, 2012-12-31 11:35:00  
> AM, 2012-12-31 12:01:00 PM, . . . .
>

Those _times_ ( _not_ Dates) cannot possibly be in %M.%S" format,  
given the number of items to the right of the decimal point that are  
greater than 60. So will proceed on the arguably more likely  
assumption that they are in fractional minutes. To recover from that  
problem, one might consider:

 > as.POSIXct(paste( floor(x), round(60*(x-floor(x))) ), format="%M %S")
  [1] "2012-12-31 00:11:00 PST" "2012-12-31 00:11:15 PST"
  [3] "2012-12-31 00:11:21 PST" "2012-12-31 00:12:01 PST"
  [5] "2012-12-31 00:11:08 PST" "2012-12-31 00:13:00 PST"
  [7] "2012-12-31 00:13:15 PST" "2012-12-31 00:13:21 PST"
  [9] "2012-12-31 00:14:01 PST" "2012-12-31 00:13:08 PST"
[11] "2012-12-31 00:14:30 PST" "2012-12-31 00:14:45 PST"
[13] "2012-12-31 00:14:51 PST" "2012-12-31 00:15:31 PST"
[15] "2012-12-31 00:14:38 PST"

-- 

David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list