【问题标题】:Plot value over hour of day with xts/zoo R使用 xts/zoo R 在一天中的一个小时内绘制值
【发布时间】:2013-04-10 05:03:49
【问题描述】:

我确实有一个看起来像这样的时间序列(分钟值):

   "timestamp", value 
    "2012-04-09 05:03:00",2
    "2012-04-09 05:04:00",4
    "2012-04-09 05:05:00",5
    "2012-04-09 05:06:00",0
    "2012-04-09 05:07:00",0
    "2012-04-09 05:08:00",3
    "2012-04-09 05:09:00",0
    "2012-04-09 05:10:00",1

有没有一种简单的方法可以在一天中的一个小时内绘制这些值:X 轴从 1 到 24 小时(或 0 和 23)。所以 - 第 5 个小时内 5:00 到 5:59 之间的所有值等等。它不取决于哪个日期,我只是对一天中的时间感兴趣。 谢谢!

附加问题:我可以将其绘制为箱线图吗?现在它是 plot(df$hh,df$coredata.._data..)in zoo 格式。

【问题讨论】:

  • 您可以通过以下方式获取小时数:as.POSIXlt(DF$timestamp)$hour。然后就这样做:plot(., DF$value) 其中. 是您从 POSIXlt 对象获得的小时数。
  • DF 已经是设置为.POSIXct 的动物园 - 所以我得到:“单变量动物园系列不可能”。
  • @Arun - 我正在创建一个答案,这正是您在评论中处理它的方式,但没有看到您已经评论过
  • zoo包不太熟悉,但也许你可以先使用strptime转换它,然后使用posixlt如下:as.POSIXlt(strptime(tt.z$timestamp, format = "%Y-%m-%d %H:%M:%S"))$hour,其中tt.z是动物园对象

标签: r time-series xts zoo


【解决方案1】:

读取创建动物园对象z的数据,然后创建hour变量并绘图:

library(zoo)

# read in data
Lines <- ' "timestamp", value 
    "2012-04-09 05:03:00",2
    "2012-04-09 05:04:00",4
    "2012-04-09 05:05:00",5
    "2012-04-09 05:06:00",0
    "2012-04-09 05:07:00",0
    "2012-04-09 05:08:00",3
    "2012-04-09 05:09:00",0
    "2012-04-09 05:10:00",1
'
z <- read.zoo(text = Lines, header = TRUE, sep = ",", tz = "")

# plot
hour <- as.POSIXlt(time(z))$hour
plot(hour, z)

【讨论】:

    【解决方案2】:

    使用xts 包,您可以使用.indexhour 泛型函数将xts 对象的索引格式化为小时。

    例如,我读取数据,

    dat <- read.zoo(text='timestamp, value 
        "2012-04-09 05:03:00",2
        "2012-04-09 05:04:00",4
        "2012-04-09 05:05:00",5
        "2012-04-09 05:06:00",0
        "2012-04-09 05:07:00",0
        "2012-04-09 05:08:00",3
        "2012-04-09 05:09:00",0
        "2012-04-09 05:10:00",1',header=TRUE,tz='',index=0:1,sep=',')
    
    transform(dat, hh = .indexhour(dat))
                        coredata.._data.. hh
    2012-04-09 05:03:00                 2  5
    2012-04-09 05:04:00                 4  5
    2012-04-09 05:05:00                 5  5
    2012-04-09 05:06:00                 0  5
    2012-04-09 05:07:00                 0  5
    2012-04-09 05:08:00                 3  5
    2012-04-09 05:09:00                 0  5
    2012-04-09 05:10:00                 1  5
    

    【讨论】:

      【解决方案3】:

      您可以通过将这些时间转换为 POSIXlt 类,然后从每个类的结果列表中提取 "hours" 组件,使用 sapply 返回一个向量来获得小时向量。然后,您可以将其添加到原始数据框中:

      df <- read.table( text = "timestamp, value 
          2012-04-09 05:03:00,2
          2012-04-09 05:04:00,4
          2012-04-09 05:05:00,5
          2012-04-09 05:06:00,0
          2012-04-09 05:07:00,0
          2012-04-09 05:08:00,3
          2012-04-09 05:09:00,0
          2012-04-09 05:10:00,1" , h = T , sep = "," )
      
      # Convert to POSIX class
      df$timestamp <- as.POSIXlt(df$timestamp)
      
      # Each entry in timestamp has some hidden attributes
      attributes(df$timestamp[1])
      $names
      # [1] "sec"   "min"   "hour"  "mday"  "mon"   "year"  "wday"  "yday"  "isdst"
      
      hours <- sapply( seq_len( nrow( df ) ) , function(x) df$timestamp[x]$hour )
      
      df$hours <- hours
      
      df
      #               timestamp value hours
      #   1 2012-04-09 05:03:00     2     5
      #   2 2012-04-09 05:04:00     4     5
      #   3 2012-04-09 05:05:00     5     5
      #   4 2012-04-09 05:06:00     0     5
      #   5 2012-04-09 05:07:00     0     5
      #   6 2012-04-09 05:08:00     3     5
      #   7 2012-04-09 05:09:00     0     5
      #   8 2012-04-09 05:10:00     1     5
      

      【讨论】:

      • 谢谢 - 效果很好,但我确实有数据已经​​在动物园(合并),如果我回到 as.data.frame(df) threre 没有更多的日期 - 我可以读小时动物园的日期格式也是如此(我通过 as.PPOISXct 设置它)。
      • 你可以用 as.POSIXlt 来设置吗?在您的问题下查看 Arun 的评论?
      • 那么我只是得到 NAs... 没有办法在 zoo 或 xts 类中吗?
      猜你喜欢
      • 2013-04-07
      • 2011-12-24
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      相关资源
      最近更新 更多