R:如何根据时间和日期查找计数

时间:2016-10-19 03:19:37

标签: r datetime count grouping

我努力实现的目标是计算一周中某小时和每小时发生的事件数量

示例数据:

Date            Time
2007-07-20   11:00:00
2007-01-05   06:15:00
2007-12-11   23:55:00
2007-12-11   23:55:00

输出:

 Day    Hour      Count
Friday  11:00 AM     1
Friday  6:00 AM      1 
Friday  11:00 PM     2

非常感谢任何帮助!

谢谢!

4 个答案:

答案 0 :(得分:1)

我喜欢使用chronlubridate包的混合来处理日期,时间和日期时间。

以下是一些示例数据:

library(dplyr)
library(chron)
library(lubridate)

df_foo = data_frame(
  date = seq.Date(from = as.Date("2016-01-01"), to = as.Date("2016-10-01"), by = "day"),
  times = chron::times(runif(n = 275, min = 0, max = 1))
)

看起来像这样:

> df_foo
# A tibble: 275 x 2
         date       times
       <date> <S3: times>
1  2016-01-01    10:26:24
2  2016-01-02    21:47:00
3  2016-01-03    15:22:06
4  2016-01-04    19:47:55
5  2016-01-05    08:51:37
6  2016-01-06    14:27:47
7  2016-01-07    17:55:59
8  2016-01-08    07:45:36
9  2016-01-09    16:52:56
10 2016-01-10    07:11:58
# ... with 265 more rows

然后,您可以按星期几和一天中的小时对它们进行分组:

df_foo %>% 
  group_by(
    `Day of Week` = lubridate::wday(date),
    `Hour of Day` = chron::hours(times)
  ) %>% 
  tally()

导致:

> df_foo %>% 
+   group_by(
+     `Day of Week` = lubridate::wday(date),
+     `Hour of Day` = chron::hours(times)
+   ) %>% 
+   tally()
Source: local data frame [137 x 3]
Groups: Day of Week [?]

   Day of Week Hour of Day     n
         <dbl>       <dbl> <int>
1            1           0     4
2            1           1     2
3            1           4     3
4            1           5     5
5            1           6     1
6            1           7     3
7            1           8     2
8            1          10     2
9            1          11     3
10           1          14     1
# ... with 127 more rows

答案 1 :(得分:1)

Base R也应对这个问题:

aggregate(
  count ~ wkday + hour,
  data=transform(dat,
                 wkday=format(as.Date(Date), "%A"),
                 hour=format(as.POSIXct(Time,format="%H:%M:%S"), "%I %p"),
                 count=1),
  FUN=sum
)
#    wkday  hour count
#1  Friday 06 AM     1
#2  Friday 11 AM     1
#3 Tuesday 11 PM     2

dat的位置:

dat <- structure(list(Date = c("2007-07-20", "2007-01-05", "2007-12-11", 
"2007-12-11"), Time = c("11:00:00", "06:15:00", "23:55:00", "23:55:00"
)), .Names = c("Date", "Time"), row.names = c(NA, -4L), class = "data.frame")

答案 2 :(得分:1)

我认为你可以使用tidyverse和函数计数。以下是解释:http://dplyr.tidyverse.org/reference/tally.html

library(tidyverse)

dat %>% group_by(Date, Time) %>% count()

# A tibble: 3 x 3
# Groups:   Date, Time [3]
        Date     Time     n
       <chr>    <chr> <int>
1 2007-01-05 06:15:00     1
2 2007-07-20 11:00:00     1
3 2007-12-11 23:55:00     2

其中:

dat <- structure(list(Date = c("2007-07-20", "2007-01-05", "2007-12-
11", 
"2007-12-11"), Time = c("11:00:00", "06:15:00", "23:55:00", "23:55:00"
)), .Names = c("Date", "Time"), row.names = c(NA, -4L), class = 
"data.frame")

答案 3 :(得分:0)

这也有效。其中df是开始的原始数据框:

df$Date <- as.Date(df$Date)
library(lubridate)
aggregate(Count~Day+Hour, data=data.frame(Day=wday(df$Date, label = TRUE, abbr = FALSE),
                 Hour=format(strptime(df$Time, format='%H:%M:%S'), '%I:00 %p'),Count=1), 
                 FUN='length')  

   Day    Hour     Count
1  Friday 06:00 AM     1
2  Friday 11:00 AM     1
3 Tuesday 11:00 PM     2