R-按日期分组并每天计算不同的值出现次数

时间:2018-10-12 09:34:32

标签: r date count

我有一个像这样的数据框:

Title         Date             Priority
title1        2018-09-03       3 Mid
title2        2018-09-03       3 Mid
title3        2018-09-03       3 Mid
title4        2018-09-03       4 High
title5        2018-09-03       3 Mid
title6        2018-09-03       3 Mid
title7        2018-09-03       3 Mid
title8        2018-09-03       2 Min
title9        2018-09-03       3 Mid
title10       2018-09-03       3 Mid
title11       2018-09-03       4 High
title12       2018-09-04       3 Mid
title13       2018-09-04       3 Mid
title14       2018-09-04       3 Mid
title15       2018-09-04       3 Mid
title16       2018-09-04       3 Mid
title17       2018-09-04       2 Min
title18       2018-09-04       3 Mid
title19       2018-09-04       4 High
title20       2018-09-04       4 High
title21       2018-09-04       3 Mid
title22       2018-09-04       4 High
title23       2018-09-04       3 Mid
title24       2018-09-04       3 Mid
title25       2018-09-04       3 Mid
title26       2018-09-04       2 Min
title27       2018-09-04       1 Low
title28       2018-09-04       3 Mid
title29       2018-09-05       4 High
title30       2018-09-05       4 High
title31       2018-09-05       3 Mid
title32       2018-09-05       3 Mid
title33       2018-09-05       3 Mid
title34       2018-09-05       3 Mid
title35       2018-09-05       3 Mid
title36       2018-09-05       3 Mid
title37       2018-09-05       3 Mid

我想按天数统计优先级出现的次数。

在输出中,我想要这样的东西:

Date         1 Low   2 Min   3 Mid   4 High   5 Critical
2018-09-03   0       1       8       2        0
2018-09-04   1       2       11      3        0
2018-09-05   0       1       10      3        0

有可能用R做到吗? 我已经尝试搜索一种解决方案,但没有找到适合我的情况的解决方案。

谢谢大家。

1 个答案:

答案 0 :(得分:0)

有多种方法可以做到这一点。如果列5 Critical在您的数据中,则会出现。这完全取决于您还想对数据做什么。

基本R:

table(df1$Date, df1$Priority)
             1 Low 2 Min 3 Mid 4 High
  2018-09-03     0     1     8      2
  2018-09-04     1     2    11      3
  2018-09-05     0     0     7      2

tidyverse:一种方法,有更多方法可以得到此结果。

    library(tidyr)
    library(dplyr)

    df1 %>% 
      group_by(Date, Priority) %>%
      summarise(n = n()) %>% 
      spread(Priority, n, fill = 0)

    # A tibble: 3 x 5
    # Groups:   Date [3]
      Date       `1 Low` `2 Min` `3 Mid` `4 High`
      <chr>        <dbl>   <dbl>   <dbl>    <dbl>
    1 2018-09-03       0       1       8        2
    2 2018-09-04       1       2      11        3
    3 2018-09-05       0       0       7        2

数据:

df1 <- structure(list(Title = c("title1", "title2", "title3", "title4", 
"title5", "title6", "title7", "title8", "title9", "title10", 
"title11", "title12", "title13", "title14", "title15", "title16", 
"title17", "title18", "title19", "title20", "title21", "title22", 
"title23", "title24", "title25", "title26", "title27", "title28", 
"title29", "title30", "title31", "title32", "title33", "title34", 
"title35", "title36", "title37"), Date = c("2018-09-03", "2018-09-03", 
"2018-09-03", "2018-09-03", "2018-09-03", "2018-09-03", "2018-09-03", 
"2018-09-03", "2018-09-03", "2018-09-03", "2018-09-03", "2018-09-04", 
"2018-09-04", "2018-09-04", "2018-09-04", "2018-09-04", "2018-09-04", 
"2018-09-04", "2018-09-04", "2018-09-04", "2018-09-04", "2018-09-04", 
"2018-09-04", "2018-09-04", "2018-09-04", "2018-09-04", "2018-09-04", 
"2018-09-04", "2018-09-05", "2018-09-05", "2018-09-05", "2018-09-05", 
"2018-09-05", "2018-09-05", "2018-09-05", "2018-09-05", "2018-09-05"
), Priority = c("3 Mid", "3 Mid", "3 Mid", "4 High", "3 Mid", 
"3 Mid", "3 Mid", "2 Min", "3 Mid", "3 Mid", "4 High", "3 Mid", 
"3 Mid", "3 Mid", "3 Mid", "3 Mid", "2 Min", "3 Mid", "4 High", 
"4 High", "3 Mid", "4 High", "3 Mid", "3 Mid", "3 Mid", "2 Min", 
"1 Low", "3 Mid", "4 High", "4 High", "3 Mid", "3 Mid", "3 Mid", 
"3 Mid", "3 Mid", "3 Mid", "3 Mid")), class = "data.frame", row.names = c(NA, 
-37L))