根据时间范围(包括分钟)对行进行子集化
Subset rows according to a range of time (incl. minutes)
我的问题基本上是对用户 Wet Feet 之前提出的问题 here 的跟进:
这是修改后的数据集:
date_time loc_id node energy kgco2
1 2009-02-27 00:11:08 87 103 0.00000 0.00000
2 2009-02-27 01:05:05 87 103 7.00000 3.75900
3 2009-02-27 02:05:05 87 103 6.40039 3.43701
4 2009-02-28 02:10:05 87 103 4.79883 2.57697
5 2009-02-28 04:05:05 87 103 4.10156 2.20254
6 2009-02-28 05:05:05 87 103 2.59961 1.39599
7 2009-03-01 03:20:05 87 103 2.59961 1.39599
我试图只获取那些在特定时间间隔内的行,例如02:05:00 到 03:30:00。
3 2009-02-27 02:05:05 87 103 6.40039 3.43701
4 2009-02-28 02:10:05 87 103 4.79883 2.57697
7 2009-03-01 03:20:05 87 103 2.59961 1.39599
应用链接问题中的解决方案(lubridate
包中的 hour
),但是,这还不够,因为我还必须考虑间隔时间。我可以使用 lubridate
包中的 interval
函数来包含分钟,但由于我的数据框涵盖不同的日期,所以它无济于事。
我特别好奇是否有任何解决方案允许使用 dplyr
的 filter
动词。还是使用 xts
包是唯一的出路?
在下面的代码中,我为每一行计算自午夜以来的秒数,并检查该值是否在您问题的时间范围内,并转换为自午夜以来的秒数。由于未以可重现的形式提供数据样本,因此我包含了使用日期时间格式(和 UTC 时区)设置数据的代码。
1.设置数据框
library(lubridate)
library(tidyverse)
dat = read.table(text="date_time time loc_id node energy kgco2
1 2009-02-27 00:11:08 87 103 0.00000 0.00000
2 2009-02-27 01:05:05 87 103 7.00000 3.75900
3 2009-02-27 02:05:05 87 103 6.40039 3.43701
4 2009-02-28 02:10:05 87 103 4.79883 2.57697
5 2009-02-28 04:05:05 87 103 4.10156 2.20254
6 2009-02-28 05:05:05 87 103 2.59961 1.39599
7 2009-03-01 03:20:05 87 103 2.59961 1.39599",
header=TRUE, stringsAsFactors=FALSE)
dat$date_time = as.POSIXct(paste0(dat$date_time, dat$time), tz="UTC")
dat = dat %>% select(-time)
2。帮助函数将 hms 时间字符串转换为自午夜以来的秒数
hms_to_numeric = function(x) {
x = as.POSIXct(paste("2010-01-01", x))
3600 * hour(x) + 60 * minute(x) + second(x)
}
3。过滤数据以仅包含时间范围
内的行
dat %>%
filter(between(as.numeric(date_time) - as.numeric(as.POSIXct(substr(date_time,1,10), tz="UTC")),
hms_to_numeric("02:05:00"),
hms_to_numeric("03:30:00")))
date_time loc_id node energy kgco2
1 2009-02-27 02:05:05 87 103 6.40039 3.43701
2 2009-02-28 02:10:05 87 103 4.79883 2.57697
3 2009-03-01 03:20:05 87 103 2.59961 1.39599
我的问题基本上是对用户 Wet Feet 之前提出的问题 here 的跟进:
这是修改后的数据集:
date_time loc_id node energy kgco2
1 2009-02-27 00:11:08 87 103 0.00000 0.00000
2 2009-02-27 01:05:05 87 103 7.00000 3.75900
3 2009-02-27 02:05:05 87 103 6.40039 3.43701
4 2009-02-28 02:10:05 87 103 4.79883 2.57697
5 2009-02-28 04:05:05 87 103 4.10156 2.20254
6 2009-02-28 05:05:05 87 103 2.59961 1.39599
7 2009-03-01 03:20:05 87 103 2.59961 1.39599
我试图只获取那些在特定时间间隔内的行,例如02:05:00 到 03:30:00。
3 2009-02-27 02:05:05 87 103 6.40039 3.43701
4 2009-02-28 02:10:05 87 103 4.79883 2.57697
7 2009-03-01 03:20:05 87 103 2.59961 1.39599
应用链接问题中的解决方案(lubridate
包中的 hour
),但是,这还不够,因为我还必须考虑间隔时间。我可以使用 lubridate
包中的 interval
函数来包含分钟,但由于我的数据框涵盖不同的日期,所以它无济于事。
我特别好奇是否有任何解决方案允许使用 dplyr
的 filter
动词。还是使用 xts
包是唯一的出路?
在下面的代码中,我为每一行计算自午夜以来的秒数,并检查该值是否在您问题的时间范围内,并转换为自午夜以来的秒数。由于未以可重现的形式提供数据样本,因此我包含了使用日期时间格式(和 UTC 时区)设置数据的代码。
1.设置数据框
library(lubridate)
library(tidyverse)
dat = read.table(text="date_time time loc_id node energy kgco2
1 2009-02-27 00:11:08 87 103 0.00000 0.00000
2 2009-02-27 01:05:05 87 103 7.00000 3.75900
3 2009-02-27 02:05:05 87 103 6.40039 3.43701
4 2009-02-28 02:10:05 87 103 4.79883 2.57697
5 2009-02-28 04:05:05 87 103 4.10156 2.20254
6 2009-02-28 05:05:05 87 103 2.59961 1.39599
7 2009-03-01 03:20:05 87 103 2.59961 1.39599",
header=TRUE, stringsAsFactors=FALSE)
dat$date_time = as.POSIXct(paste0(dat$date_time, dat$time), tz="UTC")
dat = dat %>% select(-time)
2。帮助函数将 hms 时间字符串转换为自午夜以来的秒数
hms_to_numeric = function(x) {
x = as.POSIXct(paste("2010-01-01", x))
3600 * hour(x) + 60 * minute(x) + second(x)
}
3。过滤数据以仅包含时间范围
内的行dat %>%
filter(between(as.numeric(date_time) - as.numeric(as.POSIXct(substr(date_time,1,10), tz="UTC")),
hms_to_numeric("02:05:00"),
hms_to_numeric("03:30:00")))
date_time loc_id node energy kgco2 1 2009-02-27 02:05:05 87 103 6.40039 3.43701 2 2009-02-28 02:10:05 87 103 4.79883 2.57697 3 2009-03-01 03:20:05 87 103 2.59961 1.39599