[R] Converting data.frame to xts

Ken Spriggs ksspriggs at gmail.com
Mon Jun 1 18:17:26 CEST 2009


I have a data.frame object and I don't really understand how to made an xts
class out of it.

Consider:

> x <- data.frame(datetime, ltp, ltv) 
> head(x, 2)
                 datetime ltp ltv
1 2009-05-05 07:30:01.604 899   1
2 2009-05-05 07:30:01.963 899  15
> class(x$datetime)
[1] "POSIXt"  "POSIXct"

#This works for only one column and why do I lose the name of that column?
> x0 <- xts(x$ltp, x$datetime)
> head(x0, 2)
                        [,1]
2009-05-05 07:30:01.604  899
2009-05-05 07:30:01.963  899

#The POSIXct gets indexed but then there are these 3 columns of character
strings...???
> x1 <- xts(x, x$datetime)
> head(x1, 2)
                        datetime                  ltp      ltv  
2009-05-05 07:30:01.604 "2009-05-05 12:30:01.604" "899.00" "  1"
2009-05-05 07:30:01.963 "2009-05-05 12:30:01.963" "899.00" " 15"

#Same output as x1 above
> x2 <- as.xts(x, order.by = x$datetime, datetime="POSIXct", frequency=NULL)
> head(x2, 2)
                        datetime                  ltp      ltv  
2009-05-05 07:30:01.604 "2009-05-05 12:30:01.604" "899.00" "  1"
2009-05-05 07:30:01.963 "2009-05-05 12:30:01.963" "899.00" " 15"

#Wishing it were this easy... :)
> x3 <- as.xts(x)
Error in as.POSIXlt.character(x, tz, ...) : 
  character string is not in a standard unambiguous format


-- 
View this message in context: http://www.nabble.com/Converting-data.frame-to-xts-tp23817648p23817648.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list