绘制动物园时间序列的截断时间(Plotting truncated times from zoo time series)

编程入门 行业动态 更新时间:2024-10-27 16:27:15
绘制动物园时间序列的截断时间(Plotting truncated times from zoo time series)

假设我在这些标题下有一个包含大量值的数据框:

df <- data.frame(c("Tid", "Value")) #Tid.format = %Y-%m-%d %H:%M

然后我将该数据框转到动物园,因为我想将其作为时间序列处理:

library("zoo") df <- zoo(df$Value, df$Tid)

现在我想制作一个平滑的散点图,在每个时间点进行每次测量(即丢弃日期信息,只保留时间),据说应该这样做: https : //stat.ethz.ch/pipermail/r -help / 2009年三月/ 191302.html

但似乎time()函数根本不会产生任何时间; 相反,它只是产生一个数字序列。 无论我通过该链接做什么,我都无法在平均一天获得值的散点图。 实际工作的data.frame代码(不使用动物园时间序列)看起来像这样(即从时间中提取小时并将其转换为数字):

smoothScatter(data.frame(as.numeric(format(df$Tid,"%H")),df$Value)

我想做的另一件事是生成每小时测量的密度图。 我已经使用常规data.frame绘制了几​​小时没有问题,因此我拥有的数据很好。 但是当我尝试使用动物园时,我会遇到错误,或者在尝试通过Google找到的内容时得到错误的结果。

我确实设法通过这一行绘制了一些内容:

plot(density(as.numeric(trunc(time(df),"01:00:00"))))

但这不正确。 它似乎再次产生一个从1到217的序列,我希望它能截断任何日期信息,并将时间四舍五入到几小时。

我能够绘制这个:

plot(density(df))

这会生成值的密度图。 但我想要一个密度图,记录每天每小时记录的数值。

所以,如果有人能帮我解决这个问题,那就太好了。 简而言之,我想要做的是:

1)smoothScatter(x轴:时刻(0-24),y轴:值)

2)图(密度(x轴:时间(0-24)))

编辑:

library("zoo") df <- data.frame(Tid=strptime(c("2011-01-14 12:00:00","2011-01-31 07:00:00","2011-02-05 09:36:00","2011-02-27 10:19:00"),"%Y-%m-%d %H:%M"),Values=c(50,52,51,52)) df <- zoo(df$Values,df$Tid) summary(df) df.hr <- aggregate(df, trunc(df, "hours"), mean) summary(df.hr) png("temp.png") plot(df.hr) dev.off()

这段代码是我的一些实际值。 我原本预计“df.hr”的情节是小时平均值,但我得到一些奇怪的新指数,而不是时间......

Let's say I have a data frame with lots of values under these headers:

df <- data.frame(c("Tid", "Value")) #Tid.format = %Y-%m-%d %H:%M

Then I turn that data frame over to zoo, because I want to handle it as a time series:

library("zoo") df <- zoo(df$Value, df$Tid)

Now I want to produce a smooth scatter plot over which time of day each measurement was taken (i.e. discard date information and only keep time) which supposedly should be done something like this: https://stat.ethz.ch/pipermail/r-help/2009-March/191302.html

But it seems the time() function doesn't produce any time at all; instead it just produces a number sequence. Whatever I do from that link, I can't get a scatter plot of values over an average day. The data.frame code that actually does work (without using zoo time series) looks like this (i.e. extracting the hour from the time and converting it to numeric):

smoothScatter(data.frame(as.numeric(format(df$Tid,"%H")),df$Value)

Another thing I want to do is produce a density plot of how many measurements I have per hour. I have plotted on hours using a regular data.frame with no problems, so the data I have is fine. But when I try to do it using zoo then I either get errors or I get the wrong results when trying what I have found through Google.

I did manage to get something plotted through this line:

plot(density(as.numeric(trunc(time(df),"01:00:00"))))

But it is not correct. It seems again that it is just producing a sequence from 1 to 217, where I wanted it to be truncating any date information and just keep the time rounded off to hours.

I am able to plot this:

plot(density(df))

Which produces a density plot of the Values. But I want a density plot over how many values were recorded per hour of the day.

So, if someone could please help me sort this out, that would be great. In short, what I want to do is:

1) smoothScatter(x-axis: time of day (0-24), y-axis: value)

2) plot(density(x-axis: time of day (0-24)))

EDIT:

library("zoo") df <- data.frame(Tid=strptime(c("2011-01-14 12:00:00","2011-01-31 07:00:00","2011-02-05 09:36:00","2011-02-27 10:19:00"),"%Y-%m-%d %H:%M"),Values=c(50,52,51,52)) df <- zoo(df$Values,df$Tid) summary(df) df.hr <- aggregate(df, trunc(df, "hours"), mean) summary(df.hr) png("temp.png") plot(df.hr) dev.off()

This code is some actual values that I have. I would have expected the plot of "df.hr" to be an hourly average, but instead I get some weird new index that is not time at all...

最满意答案

问题中的聚合语句有三个问题:

我们希望截断时间不是df 。

不幸的是, POSIXlt返回一个POSIXlt结果,因此需要将其转换回POSIXct

看来你不打算在第一时间截断到小时但想要提取小时数。

要解决前两点,需要将aggregate语句更改为:

tt <- as.POSIXct(trunc(time(df), "hours")) aggregate(df, tt, mean)

但要解决最后一点需要完全改变

tt <- as.POSIXlt(time(df))$hour aggregate(df, tt, mean)

There are three problems with the aggregate statement in the question:

We wish to truncate the times not df.

trunc.POSIXt unfortunately returns a POSIXlt result so it needs to be converted back to POSIXct

It seems you did not intend to truncate to the hour in the first place but wanted to extract the hours.

To address the first two points the aggregate statement needs to be changed to:

tt <- as.POSIXct(trunc(time(df), "hours")) aggregate(df, tt, mean)

but to address the last point it needs to be changed entirely to

tt <- as.POSIXlt(time(df))$hour aggregate(df, tt, mean)

更多推荐

本文发布于:2023-08-05 07:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1429709.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   序列   动物园   Plotting   truncated

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!