【问题标题】:How do I combine the first two columns and convert the data into xts format so I can analyse the time series?如何合并前两列并将数据转换为 xts 格式以便分析时间序列?
【发布时间】:2014-10-19 17:34:01
【问题描述】:
Date    Timestamp   Open    High    Low Close   Volume
1   20131020    22:00:00    1.61730 1.61730 1.61727 1.61727 0.30
2   20131020    22:01:00    1.61722 1.61727 1.61686 1.61686 23.28
3   20131020    22:02:00    1.61682 1.61707 1.61670 1.61698 41.77
4   20131020    22:03:00    1.61695 1.61701 1.61695 1.61695 9.41
5   20131020    22:04:00    1.61680 1.61680 1.61680 1.61680 4.62
6   20131020    22:05:00    1.61682 1.61698 1.61682 1.61698 2.13
7   20131020    22:06:00    1.61684 1.61706 1.61684 1.61706 9.64
8   20131020    22:07:00    1.61701 1.61701 1.61683 1.61686 7.01
9   20131020    22:08:00    1.61692 1.61693 1.61686 1.61686 6.00
10  20131020    22:09:00    1.61686 1.61692 1.61670 1.61692 3.28
11  20131020    22:10:00    1.61683 1.61683 1.61681 1.61681 2.00
12  20131020    22:11:00    1.61687 1.61687 1.61662 1.61681 3.03
13  20131020    22:12:00    1.61664 1.61688 1.61664 1.61688 5.78
14  20131020    22:13:00    1.61688 1.61695 1.61688 1.61695 8.93

如您所见,这是我在 R 中拥有的数据帧的 sn-p。如何组合前两列并将数据转换为 xts 格式,以便分析时间序列?

【问题讨论】:

    标签: r time-series xts zoo


    【解决方案1】:

    1) zoo 包中的read.zoo 使这变得相当容易。 index = 1:2 告诉它使用第 1 列和第 2 列作为索引。 tz = "" 足以让它知道您打算使用 POSIXct 索引。 (在zoo的开发版本中tz = ""可以省略,它仍然会弄清楚。)

    library(xts)
    
    z <- read.zoo(DF, index = 1:2, tz = "", format = "%Y%m%d %H:%M:%S")
    as.xts(z)
    

    2) 要获取 chron 类索引,而是将自定义 FUN 传递给它:

    library(chron)
    
    FUN = function(d, t) as.chron(paste(d, t), format = "%Y%m%d %H:%M:%S")
    z <- read.zoo(DF, index = 1:2, FUN = FUN)
    as.xts(z)
    

    3) 如果您只需要计时时间,那么所有行中的第一列都是相同的:

    library(chron)
    
    z <- read.zoo(DF[-1], FUN = times)
    as.xts(z)
    

    注意:要获得更多帮助,请尝试:?read.zoo。另请注意,此类问题非常常见,以至于在 zoo 包中使用 read.zoo 时有一个完整的小插曲:vignette("zoo-read")

    还要注意read.zoo 可以直接从文件中读取,所以如果DFread.table 的结果,你可以去掉它,只使用read.zoo

    【讨论】:

      【解决方案2】:

      试试

      indx <- as.POSIXct(paste(df[,1], df[,2]), format="%Y%m%d %H:%M:%S")
      nm1 <- c("Date", "Timestamp")
      library(xts)
      xt1 <- xts(df[setdiff(colnames(df), nm1)], order.by=indx)
      head(xt1,3)
                               Open    High     Low   Close Volume
       #2013-10-20 22:00:00 1.61730 1.61730 1.61727 1.61727   0.30
       #2013-10-20 22:01:00 1.61722 1.61727 1.61686 1.61686  23.28
       #2013-10-20 22:02:00 1.61682 1.61707 1.61670 1.61698  41.77
      

      数据

      df <- structure(list(Date = c(20131020L, 20131020L, 20131020L, 20131020L, 
       20131020L, 20131020L, 20131020L, 20131020L, 20131020L, 20131020L, 
      20131020L, 20131020L, 20131020L, 20131020L), Timestamp = c("22:00:00", 
      "22:01:00", "22:02:00", "22:03:00", "22:04:00", "22:05:00", "22:06:00", 
      "22:07:00", "22:08:00", "22:09:00", "22:10:00", "22:11:00", "22:12:00", 
      "22:13:00"), Open = c(1.6173, 1.61722, 1.61682, 1.61695, 1.6168, 
      1.61682, 1.61684, 1.61701, 1.61692, 1.61686, 1.61683, 1.61687, 
      1.61664, 1.61688), High = c(1.6173, 1.61727, 1.61707, 1.61701, 
      1.6168, 1.61698, 1.61706, 1.61701, 1.61693, 1.61692, 1.61683, 
      1.61687, 1.61688, 1.61695), Low = c(1.61727, 1.61686, 1.6167, 
      1.61695, 1.6168, 1.61682, 1.61684, 1.61683, 1.61686, 1.6167, 
      1.61681, 1.61662, 1.61664, 1.61688), Close = c(1.61727, 1.61686, 
      1.61698, 1.61695, 1.6168, 1.61698, 1.61706, 1.61686, 1.61686, 
      1.61692, 1.61681, 1.61681, 1.61688, 1.61695), Volume = c(0.3, 
      23.28, 41.77, 9.41, 4.62, 2.13, 9.64, 7.01, 6, 3.28, 2, 3.03, 
      5.78, 8.93)), .Names = c("Date", "Timestamp", "Open", "High", 
      "Low", "Close", "Volume"), class = "data.frame", row.names = c("1", 
      "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
      "14"))
      

      【讨论】:

        猜你喜欢
        • 2021-05-17
        • 1970-01-01
        • 1970-01-01
        • 2021-08-15
        • 2012-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-11
        相关资源
        最近更新 更多