【问题标题】:How to convert matrix with tick data into xts?如何将带有刻度数据的矩阵转换为xts?
【发布时间】:2016-02-28 18:09:47
【问题描述】:

我有想要转换成 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")

【问题讨论】:

    标签: r matrix xts zoo


    【解决方案1】:

    你的矩阵不整齐。查看第四列 (data[,4])。 zoo,因此 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 编码变量id、symbol、title。

    我建议您将 Message_Body 存储在单独的对象中(例如 data.frame)。

    【讨论】:

      【解决方案2】:

      根据data 的列名,您的所有数据似乎都是或可能是字符类型。但是,data[,4],GBSN.Message_Symbols 包含列表,而不是原子向量,因此我们必须使用 rbind 进行展平。然后使用apply 将每一列转换为字符向量并组合形成字符矩阵。 xts 对象是通过将行名转换为 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)))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-09
        • 1970-01-01
        • 1970-01-01
        • 2011-02-06
        • 2022-01-07
        相关资源
        最近更新 更多