【问题标题】:Calculating the number of buyers that are at every hour from 00:00 to 24:00 in the store with tidyverse使用 tidyverse 计算商店从 00:00 到 24:00 每小时的买家数量
【发布时间】:2018-07-17 15:28:54
【问题描述】:

我需要计算一天中每个小时商店里的买家数量。我已经从另一个类似的问题中复制了数据,但这似乎不能回答我正在寻找的问题。我不想计算在商店的停留时间,而是想通过计算商店中的所有买家,在一天中的每个小时计算商店的入住率。我只需要使用 tidyverse 和 lubridate 来执行此操作。

df <- structure(list(ID = c(101, 102, 103, 104, 105, 106, 107), 
                     Time_in = structure(c(1326309720, 1326309900, 1328990700, 
                                        1328997240, 1329000840, 1329004440, 
                                        1329004680), 
                    class = c("POSIXct", "POSIXt"), tzone = ""),  
                    Time_out = structure(c(1326313800, 1326317340, 1326317460, 
                                        1326324660, 1326328260, 1326335460, 
                                        1326335460), 
                    class = c("POSIXct", "POSIXt"), tzone = "")), .Names = 
                            c("ID", "Adm", "Disc"), 
                    row.names = c(NA, -7L), class = "data.frame")

【问题讨论】:

    标签: r datetime tidyverse calculation


    【解决方案1】:

    假设 Adm 和 Disc 是他们在商店中执行的操作。

    在这里使用年月日小时计数可以将其扩展到您想要的任何年份。

    df <- structure(list(ID = c(101, 102, 103, 104, 105, 106, 107), 
                         Adm = structure(c(1326309720, 1326309900, 1328990700, 
                                           1328997240, 1329000840, 1329004440, 
                                           1329004680), 
                                         class = c("POSIXct", "POSIXt"), tzone = ""),  
                         Disc = structure(c(1326313800, 1326317340, 1326317460, 
                                            1326324660, 1326328260, 1326335460, 
                                            1326335460), 
                                          class = c("POSIXct", "POSIXt"), tzone = "")), .Names = 
                      c("ID", "Adm", "Disc"), 
                    row.names = c(NA, -7L), class = "data.frame")
    
    library(tidyverse)
    library(lubridate)
    #> 
    #> Attachement du package : 'lubridate'
    #> The following object is masked from 'package:base':
    #> 
    #>     date
    by_hours <- df %>%
      gather(key = Type, Time, 2:3)
    
    by_hours
    #>     ID Type                Time
    #> 1  101  Adm 2012-01-11 20:22:00
    #> 2  102  Adm 2012-01-11 20:25:00
    #> 3  103  Adm 2012-02-11 21:05:00
    #> 4  104  Adm 2012-02-11 22:54:00
    #> 5  105  Adm 2012-02-11 23:54:00
    #> 6  106  Adm 2012-02-12 00:54:00
    #> 7  107  Adm 2012-02-12 00:58:00
    #> 8  101 Disc 2012-01-11 21:30:00
    #> 9  102 Disc 2012-01-11 22:29:00
    #> 10 103 Disc 2012-01-11 22:31:00
    #> 11 104 Disc 2012-01-12 00:31:00
    #> 12 105 Disc 2012-01-12 01:31:00
    #> 13 106 Disc 2012-01-12 03:31:00
    #> 14 107 Disc 2012-01-12 03:31:00
    
    by_hours %>%
      mutate( 
        Time = ymd_hms(Time),
        year = year(Time),
        month = month(Time),
        day = day(Time),
        hour = hour(Time),
      ) %>% 
      count(year, month, day, hour)
    #> # A tibble: 10 x 5
    #>     year month   day  hour     n
    #>    <dbl> <dbl> <int> <int> <int>
    #>  1  2012     1    11    20     2
    #>  2  2012     1    11    21     1
    #>  3  2012     1    11    22     2
    #>  4  2012     1    12     0     1
    #>  5  2012     1    12     1     1
    #>  6  2012     1    12     3     2
    #>  7  2012     2    11    21     1
    #>  8  2012     2    11    22     1
    #>  9  2012     2    11    23     1
    #> 10  2012     2    12     0     2
    

    reprex package (v0.2.0) 于 2018 年 7 月 17 日创建。

    【讨论】:

    • 抱歉,只是复制了示例,没有意识到它是 Adm - Time_in 和 Disc = Time_out。希望这是有道理的。我不确定通过计算年、月、日、小时来计算一天中每个小时商店里的人数。
    • 嘿科林,它给了我一个错误 - 计数错误(。,年,月,日,小时):未使用的参数(日,小时)
    猜你喜欢
    • 1970-01-01
    • 2015-03-11
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多