按日期方式按特定时间剪切POSIXct

Vin*_*woo 5 r time-series posixct

我有兴趣计算时间序列数据集中特定时间段的平均值.

鉴于这样的时间序列:

dtm=as.POSIXct("2007-03-27 05:00", tz="GMT")+3600*(1:240)
Count<-c(1:240)
DF<-data.frame(dtm,Count)
Run Code Online (Sandbox Code Playgroud)

在过去,我已经能够计算每日平均值

DF$Day<-cut(DF$dtm,breaks="day")
Day_Avg<-aggregate(DF$Count~Day,DF,mean)
Run Code Online (Sandbox Code Playgroud)

但现在我正试图把这一天缩短到特定的时间段,我不知道如何设置我的"休息时间".

与0:00:24:00的日平均值相反,例如,我可以获得正午至中午的平均值吗?

或者更奇特的是,我怎么能设置正午到中午的平均值,不包括晚上7点到早上6点(或者相反只包括白天的早上6点到晚上7点).

Jam*_*gle 5

让我快速重复你的代码.

dtm <- as.POSIXct("2007-03-27 05:00", tz="GMT")+3600*(1:240)
Count <- c(1:240)
DF<-data.frame(dtm,Count)

DF$Day<-cut(DF$dtm,breaks="day")
Day_Avg<-aggregate(DF$Count~Day,DF,mean)
Run Code Online (Sandbox Code Playgroud)

如果在函数调用中每次偏移12小时,则仍可以使用cut中断"day".我将保存中午到中午开始的那一天,所以我将减去12个小时.

# Get twelve hours in seconds
timeOffset <- 60*60*12
# Subtract the offset to get the start day of the noon to noon
DF$Noon_Start_Day <- cut((DF$dtm - timeOffset), breaks="day")
# Get the mean
NtN_Avg <- aggregate(DF$Count ~ Noon_Start_Day, DF, mean)
Run Code Online (Sandbox Code Playgroud)

排除特定时间的一种方法是将日期转换为POSIXlt.然后你可以访问hour其他东西.

# Indicate which times are good (use whatever boolean test is needed here)
goodTimes <- !(as.POSIXlt(DF$dtm)$hour >= 19) & !(as.POSIXlt(DF$dtm)$hour <= 6)
new_NtN_Avg <- aggregate(Count ~ Noon_Start_Day, data=subset(DF, goodTimes), mean)
Run Code Online (Sandbox Code Playgroud)

我在stackoverflow上找到了一些关于这个问题的帮助:r-calculate-means-for-subset-of-a-group


Chi*_*til 5

xts 是时间序列分析的完美包

library(xts)

originalTZ <- Sys.getenv("TZ")

Sys.setenv(TZ = "GMT")

data.xts <- as.xts(1:240, as.POSIXct("2007-03-27 05:00", tz = "GMT") + 3600 * (1:240))

head(data.xts)
##                     [,1]
## 2007-03-27 06:00:00    1
## 2007-03-27 07:00:00    2
## 2007-03-27 08:00:00    3
## 2007-03-27 09:00:00    4
## 2007-03-27 10:00:00    5
## 2007-03-27 11:00:00    6


# You can filter data using ISO-style subsetting
data.xts.filterd <- data.xts["T06:00/T19:00"]

# You can use builtin functions to apply any function FUN on daily data.
apply.daily(data.xts.filtered, mean)
##                      [,1]
## 2007-03-27 18:00:00   7.5
## 2007-03-28 18:00:00  31.5
## 2007-03-29 18:00:00  55.5
## 2007-03-30 18:00:00  79.5
## 2007-03-31 18:00:00 103.5
## 2007-04-01 18:00:00 127.5
## 2007-04-02 18:00:00 151.5
## 2007-04-03 18:00:00 175.5
## 2007-04-04 18:00:00 199.5
## 2007-04-05 18:00:00 223.5


# OR

# now let's say you want to find noon to noon average.

period.apply(data.xts, c(0, which(.indexhour(data.xts) == 11)), FUN = mean)
##                      [,1]
## 2007-03-27 11:00:00   3.5
## 2007-03-28 11:00:00  18.5
## 2007-03-29 11:00:00  42.5
## 2007-03-30 11:00:00  66.5
## 2007-03-31 11:00:00  90.5
## 2007-04-01 11:00:00 114.5
## 2007-04-02 11:00:00 138.5
## 2007-04-03 11:00:00 162.5
## 2007-04-04 11:00:00 186.5
## 2007-04-05 11:00:00 210.5


# now if you want to exclude time from 7 PM to 6 AM
data.xts.filtered <- data.xts[!data.xts %in% data.xts["T20:00/T05:00"]]

head(data.xts.filtered, 20)
##                     [,1]
## 2007-03-27 06:00:00    1
## 2007-03-27 07:00:00    2
## 2007-03-27 08:00:00    3
## 2007-03-27 09:00:00    4
## 2007-03-27 10:00:00    5
## 2007-03-27 11:00:00    6
## 2007-03-27 12:00:00    7
## 2007-03-27 13:00:00    8
## 2007-03-27 14:00:00    9
## 2007-03-27 15:00:00   10
## 2007-03-27 16:00:00   11
## 2007-03-27 17:00:00   12
## 2007-03-27 18:00:00   13
## 2007-03-27 19:00:00   14
## 2007-03-28 06:00:00   25
## 2007-03-28 07:00:00   26
## 2007-03-28 08:00:00   27
## 2007-03-28 09:00:00   28
## 2007-03-28 10:00:00   29
## 2007-03-28 11:00:00   30


period.apply(data.xts.filtered, c(0, which(.indexhour(data.xts.filtered) == 11)), FUN = mean)
##                          [,1]
## 2007-03-27 11:00:00   3.50000
## 2007-03-28 11:00:00  17.78571
## 2007-03-29 11:00:00  41.78571
## 2007-03-30 11:00:00  65.78571
## 2007-03-31 11:00:00  89.78571
## 2007-04-01 11:00:00 113.78571
## 2007-04-02 11:00:00 137.78571
## 2007-04-03 11:00:00 161.78571
## 2007-04-04 11:00:00 185.78571
## 2007-04-05 11:00:00 209.78571





Sys.setenv(TZ = originalTZ)
Run Code Online (Sandbox Code Playgroud)