【问题标题】:Getting counts grouped by hour获取按小时分组的计数
【发布时间】:2020-03-08 00:05:16
【问题描述】:

我想获取每种类型的每小时计数(version1version2)。

样本数据:

type <- c('version1','version1','version1','version2','version2')

startdate <- as.POSIXct(c('2017-11-1 02:11:02.000','2018-3-25 02:13:02.000','2019-3-14 03:45:02.000', 

                            '2017-3-14 02:55:02.000','2018-3-14 03:45:02.000'))


df <- data.frame(type, startdate)

df

      type           startdate
1 version1 2017-11-01 02:11:02
2 version1 2018-03-25 02:13:02
3 version1 2019-03-14 03:45:02
4 version2 2017-03-14 02:55:02
5 version2 2018-03-14 03:45:02

在这个df 中,我们看到version1 有两个02h 计数和一个03h 计数。

version2 02h 有一个计数,03h 有一个计数。

期望的输出:

   hour version1 version2
1 00:00        0        0
2 01:00        0        0
3 02:00        2        1
4 03:00        1        1

【问题讨论】:

    标签: r dplyr time-series aggregate tidyverse


    【解决方案1】:

    我们可以首先从startdatecount 每小时的行数和type 中获取小时数。 complete 缺少小时数并将其计数填充为 0 并使用 pivot_wider 获取宽格式数据。

    library(dplyr)
    library(tidyr)
    
    df %>%
      mutate(hr = lubridate::hour(startdate)) %>%
      count(hr, type) %>%
      complete(type, hr = seq(0, max(hr)), fill = list(n = 0)) %>%
      pivot_wider(names_from = type, values_from = n)
    
    # A tibble: 4 x 3
    #     hr version1 version2
    #  <int>    <dbl>    <dbl>
    #1     0        0        0
    #2     1        0        0
    #3     2        2        1
    #4     3        1        1
    

    【讨论】:

    • 感谢您的帮助!面临一个小问题...收到关于pivot_wider() 的错误:could not find function "pivot_wider" 我安装了tidyr,不知道这里可能出了什么问题。再次感谢!
    • @apples-oranges 它在tidyr 的较新版本中可用,您需要使用install.packages("tidyr") 安装它。如果您使用的是旧版本,请改用spreaddf %&gt;% mutate(hr = lubridate::hour(startdate)) %&gt;% count(hr, type) %&gt;% complete(type, hr = seq(0, max(hr)), fill = list(n = 0)) %&gt;% spread(type, n)
    【解决方案2】:

    您的开始日期变量有问题。因此,我使用包lubridate

    进行了设置
    library(dplyr)
    library(tidyr)
    
    type = c('version1','version1','version1','version2','version2')
    
    startdate = lubridate::ymd_hms(c('2017-11-1T02:11:02.000','2018-3-25T02:13:02.000',
                                     '2019-3-14T03:45:02.000','2017-3-14T02:55:02.000',
                                     '2018-3-14T03:45:02.000'))
    
    tibble(type = type, startdate = startdate) %>%
      count(type, hour = lubridate::hour(startdate)) %>%
      spread(type, n)
    
    # A tibble: 2 x 3
       hour version1 version2
      <int>    <int>    <int>
    1     2        2        1
    2     3        1        1
    

    【讨论】:

      【解决方案3】:

      基础 R 解决方案:

      # Extract the hour and store it as a vector: 
      
      df$hour <- gsub(".* ", "", trunc(df$startdate, units = "hours"))
      
      # Count the number of observations of each type in each hour: 
      
      df$type_hour_cnt <- with(df,
      
                              ave(paste(type, hour, sep = " - "),
      
                                  paste(type, hour, sep = " - "), FUN = seq_along))
      
      # Reshape dataframe: 
      
      df <- as.data.frame(as.matrix(xtabs(type_hour_cnt ~ hour + type, df, sparse = T)))
      
      # Extract rownames and store them as "hour" vector and then delete row.names: 
      
      df <- data.frame(cbind(hour = row.names(df), df), row.names = NULL)
      

      【讨论】:

        猜你喜欢
        • 2019-03-09
        • 2017-12-03
        • 2019-06-19
        • 1970-01-01
        • 1970-01-01
        • 2021-11-08
        • 1970-01-01
        • 1970-01-01
        • 2018-11-02
        相关资源
        最近更新 更多