根据r中的开始时间和结束时间计算白天和晚上的小时数

问题描述 投票:3回答:2

我想根据R中的开始时间和结束时间来计算白天和夜晚的分钟数。为了简化问题,我假设日出时间总是早上6点,日落时间总是下午6点,不管时间区域和位置。

以下是一些示例数据:

dat = structure(list(
  start_time = structure(c(1431096404, 1431107312, 1431124632, 1431163956, 1431170210, 1431180438, 1431225936, 1431431610, 1431434550, 1431450416, 1431457208), 
class = c("POSIXct", "POSIXt"), tzone = "America/Chicago"), 
  end_time = structure(c(1431104384, 1431119732, 1431126312, 1431168936, 1431179030, 1431193878, 1431240696, 1431432150, 1431447870, 1431455096, 1431465728),
class = c("POSIXct", "POSIXt"), tzone = "America/Chicago")), 
  row.names = c(NA, -11L), 
  class = "data.frame")

它看起来像下面的数据框:

            start_time            end_time
1  2015-05-08 09:46:44 2015-05-08 11:59:44
2  2015-05-08 12:48:32 2015-05-08 16:15:32
3  2015-05-08 17:37:12 2015-05-08 18:05:12
4  2015-05-09 04:32:36 2015-05-09 05:55:36
5  2015-05-09 06:16:50 2015-05-09 08:43:50
6  2015-05-09 09:07:18 2015-05-09 12:51:18
7  2015-05-09 21:45:36 2015-05-10 01:51:36
8  2015-05-12 06:53:30 2015-05-12 07:02:30
9  2015-05-12 07:42:30 2015-05-12 11:24:30
10 2015-05-12 12:06:56 2015-05-12 13:24:56
11 2015-05-12 14:00:08 2015-05-12 16:22:08

我想添加两列minutes_dayminutes_nightminutes_day是这一特定时期(上午6点到下午6点)的分钟数,而minutes_night是这个特定时期的分钟数(第二天下午6点到早上6点)。所以我想要的data.frame如下所示:

            start_time            end_time minutes_day minutes_night
1  2015-05-08 09:46:44 2015-05-08 11:59:44         133             0
2  2015-05-08 12:48:32 2015-05-08 16:15:32         207             0
3  2015-05-08 17:37:12 2015-05-08 18:05:12          23             5
4  2015-05-09 04:32:36 2015-05-09 05:55:36           0            83
5  2015-05-09 06:16:50 2015-05-09 08:43:50         147             0
6  2015-05-09 09:07:18 2015-05-09 12:51:18         224             0
7  2015-05-09 21:45:36 2015-05-10 01:51:36           0           246
8  2015-05-12 06:53:30 2015-05-12 07:02:30           9             0
9  2015-05-12 07:42:30 2015-05-12 11:24:30         222             0
10 2015-05-12 12:06:56 2015-05-12 13:24:56          78             0
11 2015-05-12 14:00:08 2015-05-12 16:22:08         142             0

这个问题对我来说很难,因为start_timeend_time之间的某些日期不同。

有没有人有这个问题的线索?谢谢!

r datetime lubridate
2个回答
1
投票
library(lubridate)
library(dplyr)

此函数在几分钟内创建一个从start_timeend_time的序列,提取结果时间的小时数,将它们分为白天和黑夜,并获得白天和晚上最早和最晚时间之间的时差。

get_minutes <- function(start_time, end_time) {
  mins_in_range <- seq(start_time, end_time, by = "mins")
  h_between <- hour(mins_in_range)

  hours_day <- mins_in_range[h_between >= 6 &
                     h_between < 18]
  hours_night <- mins_in_range[h_between < 6 |
                                 h_between >= 18]
  minutes_day <- tryCatch(as.numeric(difftime(max(hours_day),
                                              min(hours_day),
                                              units = "mins")),
                          warning = function(w) {
                            0
                          })

  minutes_night <- tryCatch(as.numeric(difftime(max(hours_night),
                                                min(hours_night),
                                                units = "mins")),
                            warning = function(w) {
                              0
                            })
  return(list(minutes_day = minutes_day, 
              minutes_night = minutes_night))
}

然后,您可以遍历每行数据,应用函数(返回list列)并将列表分隔为列(在data.tablerbindlist的帮助下:

dat %>% 
  rowwise() %>% 
  mutate(temp = list(get_minutes(start_time, end_time))) %>% 
  cbind(data.table::rbindlist(.$temp)) %>% 
  select(-temp)

最终结果如下:

             start_time            end_time minutes_day minutes_night
 1: 2015-05-08 09:46:44 2015-05-08 11:59:44         133             0
 2: 2015-05-08 12:48:32 2015-05-08 16:15:32         207             0
 3: 2015-05-08 17:37:12 2015-05-08 18:05:12          22             5
 4: 2015-05-09 04:32:36 2015-05-09 05:55:36           0            83
 5: 2015-05-09 06:16:50 2015-05-09 08:43:50         147             0
 6: 2015-05-09 09:07:18 2015-05-09 12:51:18         224             0
 7: 2015-05-09 21:45:36 2015-05-10 01:51:36           0           246
 8: 2015-05-12 06:53:30 2015-05-12 07:02:30           9             0
 9: 2015-05-12 07:42:30 2015-05-12 11:24:30         222             0
10: 2015-05-12 12:06:56 2015-05-12 13:24:56          78             0
11: 2015-05-12 14:00:08 2015-05-12 16:22:08         142             0

1
投票

它需要相当多的编码,但我认为这将完成这项工作。它实际上从每天的suncalc包中获得实际的日落和日出时间。

我很快就会注释它。

计算一天

这是一个以秒为单位计算日夜的功能,当开始日期和结束日期相同时。要获得确切的日出和日落时间,您需要提供该位置的纬度和经度。

library(lubridate)
library(tidyverse)
library(suncalc)

calc_in_oneday <- function(st, ed, lon = 0, lat = 0) {
    sunlight_times <- getSunlightTimes(as.Date(st), lat = lat, lon = lon)
    sunset <- sunlight_times$sunset
    sunrise <- sunlight_times$sunrise
    sec_night <- sec_day <- 0

    if(st > sunset | ed<=sunrise) { # when the period includes the night only
        sec_night  <- difftime(ed, st, units = "secs")
    } else if(st > sunrise & ed<=sunset) { # when the period includes the daytime only
        sec_day  <- difftime(ed, st, units = "secs")
    } else { # when things are bit more complicated
        if (st<=sunrise) { # if "start" is before sunrise time until sunrise will be added to night
            sec_night <- sec_night + difftime(sunrise, st, units = "secs")
        } else {  
            # if otherwise time until sunset will be added to daytime 
            # in this condition "end" will come after sunset (otherwise the second condition above will be satisfied)
            sec_day <- sec_day + difftime(sunset, st, units = "secs")
        } 
        if (ed<=sunset) { # The same logic
            sec_day <- sec_day + difftime(ed, sunrise, units = "secs")
        } else {
            sec_night <- sec_night + difftime(ed, sunset, units = "secs")
        } 
        if(st <= sunrise & ed > sunset) { # above will not add the entire daytime when "start" before sunrise and "end" after sunset
            sec_day <- sec_day + difftime(sunset, sunrise, units = "secs")
        }
    }
    sec_night <- unclass(sec_night)
    sec_day <- unclass(sec_day)
    attr(sec_day, "units") <- NULL
    attr(sec_night, "units") <- NULL
    return(list(sec_day = sec_day, sec_night = sec_night))
}

嵌套条件很复杂。我相信这是对的,但请自行检查。

For multiple days

使用上述功能,处理多天的检查。此功能的作用是检查开始日期和结束日期,以及它们是否不相同,计算直到第一个日期结束的日/夜时间,然后将开始时间滑动到第二天的开始。 (编辑:开始/结束时间的tzone)。


calc_day_night <- function(st, ed, lon = 0, lat = 0) {
    attr(st, "tzone") <- "UTC"
    attr(ed, "tzone") <- "UTC"

    sec_night <- sec_day <- 0
    while(as.Date(st) != as.Date(ed)) {
        tmp_ed <- as.Date(st) + days(1)
        day_night_oneday <- calc_in_oneday(st, tmp_ed, lon, lat)
        sec_night <- sec_night + day_night_oneday$sec_night
        sec_day <- sec_day + day_night_oneday$sec_day
        st <- tmp_ed
    }
    day_night_oneday <- calc_in_oneday(st, ed, lon, lat)
    sec_night <- sec_night + day_night_oneday$sec_night
    sec_day <- sec_day + day_night_oneday$sec_day
    return(list(sec_day = sec_day, sec_night = sec_night))
}

测试

使用测试数据,结果如下所示:

dat %>%  
    rowwise() %>%
    mutate(temp = list(calc_day_night(start_time, end_time, lat = 41, lon = -85))) %>%
    mutate(sec_day = temp$sec_day) %>%
    mutate(sec_night = temp$sec_night) %>%
    mutate(min_day = round(sec_day / 60)) %>%
    mutate(min_night = round(sec_night / 60)) %>%
    select(-matches("sec")) %>%
    select(-temp) 

## Source: local data frame [11 x 4]
## Groups: <by row>
## 
## # A tibble: 11 x 4
##    start_time          end_time            min_day min_night
##    <dttm>              <dttm>                <dbl>     <dbl>
##  1 2015-05-08 09:46:44 2015-05-08 11:59:44     133         0
##  2 2015-05-08 12:48:32 2015-05-08 16:15:32     207         0
##  3 2015-05-08 17:37:12 2015-05-08 18:05:12      28         0
##  4 2015-05-09 04:32:36 2015-05-09 05:55:36      26        57
##  5 2015-05-09 06:16:50 2015-05-09 08:43:50     147         0
##  6 2015-05-09 09:07:18 2015-05-09 12:51:18     224         0
##  7 2015-05-09 21:45:36 2015-05-10 01:51:36       0       246
##  8 2015-05-12 06:53:30 2015-05-12 07:02:30       9         0
##  9 2015-05-12 07:42:30 2015-05-12 11:24:30     222         0
## 10 2015-05-12 12:06:56 2015-05-12 13:24:56      78         0
## 11 2015-05-12 14:00:08 2015-05-12 16:22:08     142         0

我用谷歌搜索芝加哥的纬度和经度,并使用了这些值。正如你所看到的,对于一些记录,结果有点偏移(例如记录#4并不完全是夜晚,因为芝加哥的黎明是夏天的早期)。

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