R 如何分割计时日期&;zoo中用于聚合的时间对象

R 如何分割计时日期&;zoo中用于聚合的时间对象,r,R,我已将间隔五分钟的数据导入zoo对象,其中索引是一个包含日期和时间的计时: > d (09/09/09 16:45:10) 13.2 5.8 (09/09/09 16:50:10) 8.3 0.7 (09/09/09 16:55:10) 4.7 0.7 (09/09/09 17:00:10) 6.6 0.7 (09/09/09 17:05:10) 4.6 0.7 >d (09/09/09 16:45:10) 13.2 5.8 (09/09/09 16:50:10)

我已将间隔五分钟的数据导入zoo对象,其中索引是一个包含日期和时间的计时:

> d (09/09/09 16:45:10) 13.2 5.8 (09/09/09 16:50:10) 8.3 0.7 (09/09/09 16:55:10) 4.7 0.7 (09/09/09 17:00:10) 6.6 0.7 (09/09/09 17:05:10) 4.6 0.7 >d (09/09/09 16:45:10) 13.2 5.8 (09/09/09 16:50:10) 8.3 0.7 (09/09/09 16:55:10) 4.7 0.7 (09/09/09 17:00:10) 6.6 0.7 (09/09/09 17:05:10) 4.6 0.7 我试着每隔一刻钟汇总一次

我找到了一种方法,将其转换回字符串,但输出不再是一个动物园

> r =data.frame(aggregate(d,trunc(chron(times=substr(as.character(index(d)),11,18)),"00:15:00"), mean) > r 00:00:00 0.5644444 00:15:00 0.5400000 00:30:00 0.5488889 00:45:00 0.6155556 01:00:00 0.3422222 >r=数据帧(聚合(d,trunc)(时间=子字符(索引(d)),11,18)),“00:15:00”),平均值) >r 00:00:00 0.5644444 00:15:00 0.5400000 00:30:00 0.5488889 00:45:00 0.6155556 01:00:00 0.3422222
虽然我能画出这幅图,但我一直在尝试用本机实现。我发现使用zoo进行聚合可以实现白天和小时,但我无法细分小时。

我不相信内置在zoo或chron中有任何方法可以实现这一点,但您可以通过使用一点数学来创建自己的函数。给您:

trunc.minutes <- function (x, n.minutes) 
{
    if (!inherits(x, "times")) 
        x <- as.chron(x)
    x <- as.numeric(x)
    sec <- round(24 * 3600 * abs(x - floor(x)))
    h <- (sec%/%(n.minutes*60))/(60/n.minutes)
    hour <- as.integer(h)
    minutes <- (h %% hour) * 60
    chron(dates=chron::dates(x), times=times(paste(hour, minutes, "00", sep=":")))
}

我不相信有任何方法可以在zoo或chron中实现这一点,但您可以通过使用一点数学来创建自己的函数。给您:

trunc.minutes <- function (x, n.minutes) 
{
    if (!inherits(x, "times")) 
        x <- as.chron(x)
    x <- as.numeric(x)
    sec <- round(24 * 3600 * abs(x - floor(x)))
    h <- (sec%/%(n.minutes*60))/(60/n.minutes)
    hour <- as.integer(h)
    minutes <- (h %% hour) * 60
    chron(dates=chron::dates(x), times=times(paste(hour, minutes, "00", sep=":")))
}

考虑到Shane的想法,我对它做了一点修改…最初的问题是如何在所涉及的小时数中累积分钟数,并去掉日期。另外,由于数学不喜欢早些时候的午夜,我使用字符串解析

# Where X is a zoo obj with chron timestamps containing both time & date # and min is like "00:30:00" for half hour intervals > trunc.chrontime = function (x, min) { if (!inherits(x, "times")) x = as.chron(x) s = substr(as.character(x),11,18) c = chron(times=s) trunc(c,min) } > s = aggregate(d,trunc.minstr(index(d),"00:30:00"),mean) s 00:00:00 0.5522222 0.4988889 0.006666667 00:30:00 0.5822222 0.5366667 0.012222222 01:00:00 0.3388889 0.4455556 0.000000000 01:30:00 0.3422222 0.4344444 0.000000000 02:00:00 0.3366667 0.4366667 0.000000000 ... #其中X是一个zoo obj,带有包含时间和日期的计时时间戳 #分钟就像是“00:30:00”,每半小时一次 >trunc.chrontime=功能(x,min) { 如果(!继承(x,“次”)) x=as.chron(x) s=子字符(如字符(x),11,18) c=计时(时间=秒) trunc(c,min) } >s=骨料(d,trunc.minstr(指数(d),“00:30:00”),平均值) s 00:00:00 0.5522222 0.4988889 0.006666667 00:30:00 0.5822222 0.5366667 0.012222222 01:00:00 0.3388889 0.4455556 0.000000000 01:30:00 0.3422222 0.4344444 0.000000000 02:00:00 0.3366667 0.4366667 0.000000000 ...
考虑到Shane的想法,我对它做了一点修改…最初的问题是如何在所涉及的小时数中累积分钟数,并去掉日期。另外,由于数学不喜欢早些时候的午夜,我使用字符串解析

# Where X is a zoo obj with chron timestamps containing both time & date # and min is like "00:30:00" for half hour intervals > trunc.chrontime = function (x, min) { if (!inherits(x, "times")) x = as.chron(x) s = substr(as.character(x),11,18) c = chron(times=s) trunc(c,min) } > s = aggregate(d,trunc.minstr(index(d),"00:30:00"),mean) s 00:00:00 0.5522222 0.4988889 0.006666667 00:30:00 0.5822222 0.5366667 0.012222222 01:00:00 0.3388889 0.4455556 0.000000000 01:30:00 0.3422222 0.4344444 0.000000000 02:00:00 0.3366667 0.4366667 0.000000000 ... #其中X是一个zoo obj,带有包含时间和日期的计时时间戳 #分钟就像是“00:30:00”,每半小时一次 >trunc.chrontime=功能(x,min) { 如果(!继承(x,“次”)) x=as.chron(x) s=子字符(如字符(x),11,18) c=计时(时间=秒) trunc(c,min) } >s=骨料(d,trunc.minstr(指数(d),“00:30:00”),平均值) s 00:00:00 0.5522222 0.4988889 0.006666667 00:30:00 0.5822222 0.5366667 0.012222222 01:00:00 0.3388889 0.4455556 0.000000000 01:30:00 0.3422222 0.4344444 0.000000000 02:00:00 0.3366667 0.4366667 0.000000000 ...
chron有一个trunc.times方法,因此我们可以这样做:

library(zoo)
library(chron)
z <- zoo(1:5, 
   chron(c("02/27/92", "02/27/92", "01/14/92", "02/28/92", "02/01/92"),
   c("23:03:20", "23:29:56", "01:03:30", "18:21:03", "16:56:26")))
aggregate(z, function(x) trunc(x, "00:15:00"), mean)
图书馆(动物园)
图书馆(计时)

zchron有一个trunc.times方法,因此我们可以这样做:

library(zoo)
library(chron)
z <- zoo(1:5, 
   chron(c("02/27/92", "02/27/92", "01/14/92", "02/28/92", "02/01/92"),
   c("23:03:20", "23:29:56", "01:03:30", "18:21:03", "16:56:26")))
aggregate(z, function(x) trunc(x, "00:15:00"), mean)
图书馆(动物园)
图书馆(计时)

z我同意似乎没有一个内置的时间方法,只有日期。我只是注意到数学不喜欢军事时间(00小时结果为NA)。也许我使用的字符串方法将与包装函数一起工作,使其返回到计时。我更新了我的示例。您可以在zoo对象上使用聚合,如上所述。我同意似乎没有内置的时间方法,只有日期。我只是注意到数学不喜欢军事时间(小时00结果为NA)。也许我使用的字符串方法将与包装器函数一起工作,使其返回到一个计时。我更新了我的示例。您可以在zoo对象上使用聚合,如上所述。这也很有效。您在问题中没有提到删除日期,尽管更改我的方法很容易做到这一点(只是从函数返回时间,而不是从chron对象返回时间)。你是对的,尽管在我的示例数据中我试图说明这一点。这是我最初问题的一半。谢谢。这也很有效。你在问题中没有提到删除日期,尽管更改我的日期很容易做到这一点(只是从函数返回时间,而不是从chron对象返回时间)。你是正确的,尽管在我的示例数据中我试图说明这一点。这是我最初问题的一半。谢谢。