R 中的分叉计数基础日期时间

bifurcate count basis datetime in R

我在 R 中有下面提到的数据框。

DF

ID          Datetime                Value
T-1         2020-01-01 15:12:14     10
T-2         2020-01-01 00:12:10     20
T-3         2020-01-01 03:11:11     25
T-4         2020-01-01 14:01:01     20
T-5         2020-01-01 18:07:11     10
T-6         2020-01-01 20:10:09     15
T-7         2020-01-01 15:45:23     15

通过利用上述数据框,考虑到 Datetime

,我想将计数基础月份和时间段分叉

所需输出:

Month                   Count              Sum
Jan-20                   7                 115
12:00 AM to 05:00 AM     2                 45
06:00 AM to 12:00 PM     0                 0
12:00 PM to 03:00 PM     1                 20
03:00 PM to 08:00 PM     3                 35
08:00 PM to 12:00 AM     1                 15

您可以使用 lubridate 包中的 hour,然后使用基础 R 中的 cut,然后使用 dplyr 进行汇总。

在这里,我假设您的日期时间列实际上是日期时间格式,而不仅仅是字符串或因子。如果是,请确保您已先完成 DF$Datetime <- as.POSIXct(as.character(DF$Datetime)) 转换。

library(tidyverse)

DF$bins <- cut(lubridate::hour(DF$Datetime), c(-1, 5.99, 11.99, 14.99, 19.99, 24))
levels(DF$bins) <- c("00:00 to 05:59", "06:00 to 11:59", "12:00 to 14:59", 
                     "15:00 to 19:59", "20:00 to 23:59")

newDF <- DF %>% 
         group_by(bins, .drop = FALSE) %>% 
         summarise(Count = length(Value), Total = sum(Value))

结果如下:

newDF
#> # A tibble: 5 x 3
#>   bins           Count Total
#>   <fct>          <int> <dbl>
#> 1 00:00 to 05:59     2    45
#> 2 06:00 to 11:59     0     0
#> 3 12:00 to 14:59     1    20
#> 4 15:00 to 19:59     3    35
#> 5 20:00 to 23:59     1    15

如果您想将一月添加为第一行(尽管我不确定这在这种情况下有多大意义),您可以这样做:

newDF %>% 
summarise(bins = "January", Count = sum(Count), Total = sum(Total)) %>% bind_rows(newDF)
#> # A tibble: 6 x 3
#>   bins           Count Total
#>   <chr>          <int> <dbl>
#> 1 January            7   115
#> 2 00:00 to 05:59     2    45
#> 3 06:00 to 11:59     0     0
#> 4 12:00 to 14:59     1    20
#> 5 15:00 to 19:59     3    35
#> 6 20:00 to 23:59     1    15

顺便说一句,我为此使用的数据的可重现版本是:

structure(list(ID = structure(1:7, .Label = c("T-1", "T-2", "T-3", 
"T-4", "T-5", "T-6", "T-7"), class = "factor"), Datetime = structure(c(1577891534, 
1577837530, 1577848271, 1577887261, 1577902031, 1577909409, 1577893523
), class = c("POSIXct", "POSIXt"), tzone = ""), Value = c(10, 
20, 25, 20, 10, 15, 15)), class = "data.frame", row.names = c(NA, 
-7L))