将 R 中的时间数据绘制为各种分辨率(分钟、小时、秒等) [英] Plot time data in R to various resolutions (to the minute, to the hour, to the second, etc.)

查看:41
本文介绍了将 R 中的时间数据绘制为各种分辨率(分钟、小时、秒等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 CSV 格式的数据,例如:

时间戳",计数"《2009-07-20 16:30:45》,10《2009-07-20 16:30:45》,15"2009-07-20 16:30:46", 8"2009-07-20 16:30:46", 6"2009-07-20 16:30:46", 8《2009-07-20 16:30:47》,20

我可以使用 read.cvs 将其读入 R.我想绘制:

  1. 每秒条目数,因此:<前>"2009-07-20 16:30:45", 2"2009-07-20 16:30:46", 3《2009-07-20 16:30:47》, 1

  2. 每秒平均值:<前>《2009-07-20 16:30:45》,12.52009-07-20 16:30:46",7.333《2009-07-20 16:30:47》,20

  3. 同 1 &2 然后按分钟,然后按小时.

在 R 中有什么方法可以做到这一点(按秒/分钟/等& 情节收集)?

解决方案

读取您的数据,并将其转换为 动物园 对象:

R>X <- read.csv("/tmp/so.csv")R>X <- zoo(X$Count, order.by=as.POSIXct(as.character(X[,1])))

请注意,由于时间戳不唯一,这将显示警告.

任务 1 使用 aggregatelength 进行计数:

R>聚合(X,力,长度)2009-07-20 16:30:45 2009-07-20 16:30:46 2009-07-20 16:30:472 3 1

任务 2 使用 aggregate:

R>聚合(X,力,平均值)2009-07-20 16:30:45 2009-07-20 16:30:46 2009-07-20 16:30:4712.500 7.333 20.000

任务 3 可以通过聚合高阶索引来完成.您可以对聚合的结果调用 plot:

plot(aggregate(X, force, mean))

I have some data in CSV like:

"Timestamp", "Count"
"2009-07-20 16:30:45", 10
"2009-07-20 16:30:45", 15
"2009-07-20 16:30:46", 8
"2009-07-20 16:30:46", 6
"2009-07-20 16:30:46", 8
"2009-07-20 16:30:47", 20

I can read it into R using read.cvs. I'd like to plot:

  1. Number of entries per second, so:

    "2009-07-20 16:30:45", 2
    "2009-07-20 16:30:46", 3
    "2009-07-20 16:30:47", 1
    

  2. Average value per second:

    "2009-07-20 16:30:45", 12.5
    "2009-07-20 16:30:46", 7.333
    "2009-07-20 16:30:47", 20
    

  3. Same as 1 & 2 but then by Minute and then by Hour.

Is there some way to do this (collect by second/min/etc & plot) in R?

解决方案

Read your data, and convert it into a zoo object:

R> X <- read.csv("/tmp/so.csv")
R> X <- zoo(X$Count, order.by=as.POSIXct(as.character(X[,1])))

Note that this will show warnings because of non-unique timestamps.

Task 1 using aggregate with length to count:

R> aggregate(X, force, length)
2009-07-20 16:30:45 2009-07-20 16:30:46 2009-07-20 16:30:47 
                  2                   3                   1 

Task 2 using aggregate:

R> aggregate(X, force, mean)
2009-07-20 16:30:45 2009-07-20 16:30:46 2009-07-20 16:30:47 
             12.500               7.333              20.000 

Task 3 can be done the same way by aggregating up to higher-order indices. You can call plot on the result from aggregate:

plot(aggregate(X, force, mean))

这篇关于将 R 中的时间数据绘制为各种分辨率(分钟、小时、秒等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆