How can i convert a dataframe with a factor column to a xts object?

怎甘沉沦 提交于 2019-12-06 08:58:36

UPDATE: From your edits, it appears you imported your data using two different commands. It also appears you should be using read.csv2. I've updated my answer with Lines that (I assume) look more like your original CSV (I have to guess because you don't say what the file looks like). The rest of the answer doesn't change.

You have to add a date to your times because xts stores all index values internally as POSIXct (I just used today's date).

I had to convert the "," decimal notation to the "." convention (using gsub), but that may be locale-dependent and you may not need to. paste today's date with the (possibly converted) time and then convert it to POSIXct to create an index suitable for xts.

I've also formatted the index so you can see the fractional seconds.

Lines <- "Time;Close;Volume
10:27:03,6;0,99;1000
10:32:58,4;0,98;100
10:34:16,9;0,98;600
10:35:46,0;0,97;500
10:35:50,6;0,96;50
10:35:50,6;0,96;1000
10:36:10,3;0,95;40
10:36:10,3;0,95;100
10:36:10,4;0,95;500
10:36:10,4;0,95;100"

SOLK <- read.csv2(con <- textConnection(Lines))
close(con)

solk <- xts(SOLK[,c("Close","Volume")],
  as.POSIXct(paste("2011-09-02", gsub(",",".",SOLK[,1]))))
indexFormat(solk) <- "%Y-%m-%d %H:%M:%OS6"
solk
#                            Close Volume
# 2011-09-02 10:27:03.599999  0.99   1000
# 2011-09-02 10:32:58.400000  0.98    100
# 2011-09-02 10:34:16.900000  0.98    600
# 2011-09-02 10:35:46.000000  0.97    500
# 2011-09-02 10:35:50.599999  0.96     50
# 2011-09-02 10:35:50.599999  0.96   1000
# 2011-09-02 10:36:10.299999  0.95     40
# 2011-09-02 10:36:10.299999  0.95    100
# 2011-09-02 10:36:10.400000  0.95    500
# 2011-09-02 10:36:10.400000  0.95    100

That's an odd structure. Translating it to dput syntax

SOLK <- structure(list(structure(c(1L, 2L, 3L, 4L, 5L, 5L, 6L, 6L, 7L, 
7L), .Label = c("10:27:03,6", "10:32:58,4", "10:34:16,9", "10:35:46,0", 
"10:35:50,6", "10:36:10,3", "10:36:10,4"), class = "factor"), 
    Close = c(0.99, 0.98, 0.98, 0.97, 0.96, 0.96, 0.95, 0.95, 
    0.95, 0.95), Volume = c(1000L, 100L, 600L, 500L, 50L, 1000L, 
    40L, 100L, 500L, 100L)), .Names = c("", "Close", "Volume"
), class = "data.frame", row.names = c("1", "2", "3", "4", "5", 
"6", "7", "8", "9", "10"))

I'm assuming the comma in the timestamp is decimal separator.

library("chron")
time.idx <- times(gsub(",",".",as.character(SOLK[[1]])))

Unfortunately, it seems xts won't take this as a valid order.by; so a date (today, for lack of a better choice) must be included to make xts happy.

xts(SOLK[[2]], order.by=chron(Sys.Date(), time.idx))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!