簡體   English   中英

r - 按年或星期幾計算間隔的小時數

[英]r - counting hours of intervals by year or day of week

想象一張表,上面有一些警察的工作日期和時間。 我們有每個班次的初始日期時間和最終日期時間。 例如,我想計算每年的小時數。 我可以做到這一點,但這是一項相當復雜的任務,因為 2 年不同的時間會發生變化。 有沒有一種快速的方法來做到這一點? 這看起來是一項非常常見的任務,但我找不到任何包來做到這一點。 例如,如果我想計算一周中某天的工作時間,我會遇到同樣的問題。 有什么跡象嗎?

數據:

library(tidyverse)
library(lubridate)

tabela <- tibble(
  data_hora_chegada = c(
    ymd_hms("2018-07-05 18:00:00"),
    ymd_hms("2019-05-05 20:00:00"),
    ymd_hms("2019-12-31 23:00:00")
  ),
  data_hora_saida = c(
    ymd_hms("2018-07-06 16:00:00"),
    ymd_hms("2019-05-05 22:30:00"),
    ymd_hms("2020-01-01 15:00:00")
  )
)

tabela %>%
  mutate(
    intervalo = lubridate::interval(
      data_hora_chegada,
      data_hora_saida
    )
  ) -> tabela

預期輸出:

Year|Hours
2018| 22
2019| 3,5
2020| 15

# or similarly, with "Day of Week" instead of "Year"

為了正確計算年末,您基本上需要將您的記錄分成兩部分,然后您可以利用您已經編寫的代碼執行intervalo的計算,然后進行總結。

tabela.split <-
  tabela %>%
  rowwise() %>%
  do(
    {
      my_row <- .

      new_df <- data.frame(
        Year = year(my_row$data_hora_chegada) : year(my_row$data_hora_saida)
      ) %>%
        mutate(
          data_hora_chegada = ISOdate(Year, 1, 1, 0, 0, 0),
          data_hora_saida = ISOdate(Year+1, 1, 1, 0, 0, 0)
        )

      new_df[1, "data_hora_chegada"] <- my_row$data_hora_chegada
      new_df[nrow(new_df), "data_hora_saida"] <- my_row$data_hora_saida

      new_df
    }
  ) %>%
  ungroup()

print(tabela.split)

這是代碼已准備好用於任何間隔,因此如果間隔中有兩年以上,它將為間隔跨度中的所有年份生成所需的盡可能多的記錄。

輸出

   Year data_hora_chegada   data_hora_saida    
* <int> <dttm>              <dttm>             
1  2018 2018-07-05 18:00:00 2018-07-06 16:00:00
2  2019 2019-05-05 20:00:00 2019-05-05 22:30:00
3  2019 2019-12-31 23:00:00 2020-01-01 00:00:00
4  2020 2020-01-01 00:00:00 2020-01-01 15:00:00

現在您可以使用group_by()group_by() summarize()來獲得按年份的總數。

tabela.split %>%
  mutate(
    intervalo = as.numeric(lubridate::interval(
      data_hora_chegada,
      data_hora_saida
    ))
  ) %>%
  group_by(Year) %>%
  summarise(Hours = round(sum(intervalo)/3600,1))

輸出

   Year Hours
  <dbl> <dbl>
1  2018  22  
2  2019   3.5
3  2020  15  

然而,在任何時間測量動態指定的情況下使其中斷將是一項非常復雜的任務。

這是另一種方法。 您可以查看每個日歷年與您在tabela確定的間隔之間的tabela

# Create a vector of years, from minimum to maximum
all_years <- with(tabela, seq(min(year(data_hora_chegada)), max(year(data_hora_saida))))

# Create list of year intervals for these years
year_intervals <- lapply(all_years, function(x) interval(ymd_hms(paste0(x, "-01-01 00:00:00")), ymd_hms(paste0(x+1, "-01-01 00:00:00"))))
names(year_intervals) <- all_years

# Determine overlap/intersection between single year intervals and tabela intervals
year_overlaps <- lapply(year_intervals, intersect, tabela$intervalo)

# Convert to data frame and sum hours for each year
data.frame(Hours = rowSums(t(sapply(year_overlaps, c)), na.rm = TRUE)/3600)

輸出

     Hours
2018  22.0
2019   3.5
2020  15.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM