如何按照特定的兴趣时段对数据进行分组?

问题描述 投票:0回答:1

我有一个在五个日期在特定日期时间获取的温度值的数据集。出于某种原因,有时读数是每小时一次,有些是每四个小时一次。另一个问题是,由于夏时制而导致时间改变时,读数会减少一小时。我对每四个小时获取的读数很感兴趣,并希望将它们白天和黑夜细分,以最终获得每天和每晚的平均温度。

总结一下,我感兴趣的读数之一是:

0800, 1200, 1600 =day
2000, 0000, 0400 =night

每天的记录介于0800-1600和2000-0400之间。

夏令时期间,等效时间为:

0900, 1300, 1700 =day
2100, 0100, 0500 =night

每天的记录介于0900-1700和2100-0500之间。

在此过程中,我希望按站点进行子集划分。还有一些NA值或空白单元格应忽略。

到目前为止,我尝试按一个小时的时间兴趣进行分组,以查看其是否有效,但除此之外,别无所求。关于如何按一系列感兴趣的时间进行子集化的任何技巧?谢谢!

temperature <- read.csv("SeaTemperatureData.csv", 
                      stringsAsFactors = FALSE) 
temperature <- subset(temperature, select=-c(X)) #remove last column that contains comments, not needed
temperature$Date.Time < -as.POSIXct(temperature$Date.Time,
                                    format="%d/%m/%Y %H:%M",
                                    tz="Pacific/Auckland")
#subset data by time, we only want to include temperatures recorded at certain times
temperature.goat <- subset(temperature, Date.Time==c('01:00:00'), select=c("Goat.Island"))
r datetime subset temperature
1个回答
0
投票

一种可能的解决方案是从DateTime变量中提取小时,然后过滤特定的小时数。

这里有一个为期4天的伪造示例:

library(lubridate)

df <- data.frame(DateTime = seq(ymd_hms("2020-02-01 00:00:00"), ymd_hms("2020-02-05 00:00:00"), by = "hour"),
                 Value = sample(1:100,97, replace = TRUE))

             DateTime Value
1 2020-02-01 00:00:00    99
2 2020-02-01 01:00:00    51
3 2020-02-01 02:00:00    44
4 2020-02-01 03:00:00    49
5 2020-02-01 04:00:00    60
6 2020-02-01 05:00:00    56

现在,您可以使用hourlubridate功能提取小时,并提取所需小时的子集:

library(lubridate)

subset(df, hour(DateTime) == 5)

              DateTime Value
6  2020-02-01 05:00:00    56
30 2020-02-02 05:00:00    31
54 2020-02-03 05:00:00    65
78 2020-02-04 05:00:00    80

它回答了您的问题吗?

© www.soinside.com 2019 - 2024. All rights reserved.