[R] Lost in POSIX

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Sun Dec 26 19:02:58 CET 2010


Dimitri Shvorob wrote:
> .. One issue with the solution proposed by Jeff is that the transformed
> column does not have the original's type:
> 
>> x = structure(list(time = structure(c(1020232904.818, 1020232904.818
> ), class = c("POSIXt", "POSIXct"), tzone = ""), price = c(321, 
> 323.5), minute = c(1020232860, 1020232860)), .Names = c("time", 
> "price", "minute"), row.names = 1:2, class = "data.frame")
> 
> minute <- function(t)
> { 
>   d <- as.POSIXlt(t, origin = as.Date("1970-01-01")) 
>   d$sec <- 0 
>   as.POSIXct(d) 
> } 
> 
>> x$minute = sapply(x$time, minute)  
> 
>> head(x)
>                  time price     minute
> 1 2002-05-01 07:01:44 321.0 1020232860
> 2 2002-05-01 07:01:44 323.5 1020232860
> 
>> class(x.l$minute)
> [1] "numeric"
> 

That is not an issue with the "minute" function, as you can see if you
evaluate

 > minute(x$time)
[1] "2002-04-30 23:01:00 PDT" "2002-04-30 23:01:00 PDT"

or

 > str(minute(x$time))
  POSIXct[1:2], format: "2002-04-30 23:01:00" "2002-04-30 23:01:00"

rather, you are seeing a side effect of "sapply".

-- 
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k



More information about the R-help mailing list