2016-02-28 63 views
-1

我有我試圖轉換成XTS格式的數據:如何將矩陣與刻度數據轉換爲xts?

> dput(data) 
structure(list(50370788L, 50370777L, 50370694L, 50370620L, 50370504L, 
    620639L, 620639L, 592639L, 592639L, 592639L, "2015-10-24", 
    "2015-10-24", "2015-09-04", "2015-09-04", "2015-09-04", structure(list(
     id = 12544L, symbol = "GBSN", title = "Great Basin Scientific, Inc."), .Names = c("id", 
    "symbol", "title"), class = "data.frame", row.names = 1L), 
    structure(list(id = 12544L, symbol = "GBSN", title = "Great Basin Scientific, Inc."), .Names = c("id", 
    "symbol", "title"), class = "data.frame", row.names = 1L), 
    structure(list(id = 12544L, symbol = "GBSN", title = "Great Basin Scientific, Inc."), .Names = c("id", 
    "symbol", "title"), class = "data.frame", row.names = 1L), 
    structure(list(id = 12544L, symbol = "GBSN", title = "Great Basin Scientific, Inc."), .Names = c("id", 
    "symbol", "title"), class = "data.frame", row.names = 1L), 
    structure(list(id = 12544L, symbol = "GBSN", title = "Great Basin Scientific, Inc."), .Names = c("id", 
    "symbol", "title"), class = "data.frame", row.names = 1L), 
    "$GBSN Still sticking with my prediction of FDA coming sometime in March..", 
    "$GBSN Last time I check NASDAQ gave them till sometime in April to get it together or else they'll see pink. Correct me if in wrong?", 
    "$GBSN time for retailers to get knocked out of the ring with a 25 to 30 % gain", 
    "$GBSN market cap will end up around 65 million not enough to comply rs takes it to 21 dollars pps 26$ by august", 
    "$GBSN shorts are going to attack the sell off"), .Dim = c(5L, 
5L), .Dimnames = list(c("2016-02-28 16:59:53", "2016-02-28 16:58:58", 
"2016-02-28 16:51:36", "2016-02-28 16:46:09", "2016-02-28 16:34:34" 
), c("GBSN.Message_ID", "GBSN.User_ID", "GBSN.User_Join_Date", 
"GBSN.Message_Symbols", "GBSN.Message_Body"))) 

我一直在嘗試使用:

Message_series <- xts(zoo(data, format='%Y-%m-%d %H:%M:%S')) 

我得到這個錯誤:

Error in zoo(data, format = "%Y-%m-%d %H:%M:%S") : 
    unused argument (format = "%Y-%m-%d %H:%M:%S") 

回答

0

你矩陣不整齊。看看第四列(data[,4])。動物園,因此xts不支持如此複雜的對象只有簡單的矩陣與所有元素在同一類型。

第一欄和第二欄都可以。他們繼承了列表屬性,所以轉換並不那麼簡單。

data.mat <- matrix(as.numeric(data[,1:2]), ncol = 2) 
colnames(data.mat) <- colnames(data)[1:2] 
xts(data.mat, order.by = as.POSIXct(rownames(data))) 

加入數據可以轉換,其中包括:

data.mat <- cbind(data.mat, as.numeric(as.Date(as.character(data[,3])))) 
colnames(data.mat) <- colnames(data)[1:3] 
data.xts <- xts(data.mat, order.by = as.POSIXct(rownames(data))) 

和可轉換回:

as.Date(coredata(data.xts['2016-02-28 16:59:53',3])) 

你也能以同樣的方式代碼Message_Symbols變量idsymboltitle

我建議您將Message_Body存儲在單獨的對象(如data.frame)中。

0

根據列名稱data,看起來您的所有數據都是或可能是字符類型。但是,data[,4],GBSN.Message_Symbols包含列表,而不是原子向量,因此我們必須使用rbind來展平。然後使用apply將每列轉換爲字符向量並組合以形成字符矩陣。 xts對象是通過將rownames轉換爲POSIX日期/時間類型並將它們用作索引而形成的。代碼看起來像

# flatten list data in column 4 to a data frame 
    mat4 <- do.call(rbind, data[,4]) 
# convert all data to character type 
    data.mat <- apply(cbind(data[,-4], mat4), 2, as.character)  
# create xts time series 
    data.xts <- xts(data.mat, order.by = as.POSIXct(rownames(data)))