我希望创建一个数据集,该数据集仅包含每一次观测4小时之间的数据。目前,我拥有一个每小时观察的数据集。分割不起作用,因为样品之间没有4小时的间隔。
IndividId DateTimeLMT isDaylight YEAR month day hour
<chr> <dttm> <chr> <dbl> <dbl> <dbl> <dbl>
1 M_15_03 2017-01-13 15:00:42 True 2017 1 13 15
2 M_15_03 2017-01-13 16:00:14 False 2017 1 13 16
3 M_15_03 2017-01-13 17:00:09 False 2017 1 13 17
4 M_15_03 2017-01-13 18:00:42 False 2017 1 13 18
5 M_15_03 2017-01-13 19:00:14 False 2017 1 13 19
6 M_15_03 2017-01-13 20:00:45 False 2017 1 13 20 我所要寻找的结果是一个公式,它将创建类似于以下内容的内容:
IndividId DateTimeLMT isDaylight YEAR month day hour
<chr> <dttm> <chr> <dbl> <dbl> <dbl> <dbl>
1 M_15_03 2017-01-13 15:00:42 True 2017 1 13 15
2 M_15_03 2017-01-13 19:00:14 False 2017 1 13 19
3 M_15_03 2017-01-13 23:00:09 False 2017 1 13 23
4 M_15_03 2017-01-14 03:00:42 False 2017 1 14 03
5 M_15_03 2017-01-14 07:00:14 False 2017 1 14 07
6 M_15_03 2017-01-14 11:00:45 True 2017 1 14 11 发布于 2022-03-02 13:41:10
您可以使用lubridate::floor_date。floor_date设置为4 hours将在16 - 20 - 24小时内旋转,因此您可以使用铅来制作15-19,并填充tidyr::fill。
library(lubridate)
library(tidyr)
library(dplyr)
data %>%
mutate(fl = floor_date(lead(DateTimeLMT), unit = "4 hours")) %>%
fill(fl) %>%
group_by(IndividId, fl) %>%
summarise(across(c(DateTimeLMT, YEAR:hour), first),
isDaylight = any(isDaylight == "True"))
# A tibble: 2 x 8
# Groups: IndividId [1]
IndividId fl DateTimeLMT YEAR month day hour isDaylight
<chr> <dttm> <dttm> <int> <int> <int> <int> <lgl>
1 M_15_03 2017-01-13 16:00:00 2017-01-13 15:00:42 2017 1 13 15 TRUE
2 M_15_03 2017-01-13 20:00:00 2017-01-13 19:00:14 2017 1 13 19 FALSE 数据
data <- read.table(header = T, text = "IndividId DateTimeLMT isDaylight YEAR month day hour
1 M_15_03 2017-01-13-15:00:42 True 2017 1 13 15
2 M_15_03 2017-01-13-16:00:14 False 2017 1 13 16
3 M_15_03 2017-01-13-17:00:09 False 2017 1 13 17
4 M_15_03 2017-01-13-18:00:42 False 2017 1 13 18
5 M_15_03 2017-01-13-19:00:14 False 2017 1 13 19
6 M_15_03 2017-01-13-20:00:45 False 2017 1 13 20 ")https://stackoverflow.com/questions/71323443
复制相似问题