分组依据和计数字符值 R

Group by and Count character values R

我需要计算每个日期有多少订单以及每个日期有多少“是”和“否”。 我怎样才能从这样的数据框中:

  order <- c("order_1",  "order_2","order_3","order_4","order_5")
  check <- c("yes",  "no","no","yes","yes")
  df1 <-  data.frame(order,  date,check )
  df1


    order   date      check
1 order_1 2020-01-01   yes
2 order_2 2020-01-01    no
3 order_3 2020-01-01    no
4 order_4 2020-01-02   yes
5 order_5 2020-01-02   yes


这样得到table:

    date      number_orders  yes  no
1 2020-01-01       3          1    2
2 2020-01-02       2          0    0

´´´

首先将 yesno 列添加到数据框中。让 yescheck == 'yes' 时为真,否则为假,让 nocheck == 'no' 时为真,否则为假。

df1 <- df1 %>% mutate(yes = (check == 'yes'), no = (check == 'no'))

现在按日期分组,用summarize()统计订单数。您可以利用 TRUE 计算结果为 1 而 FALSE 计算结果为 0 的事实来对 yesno 列求和。

df1 <- df1 %>% 
  group_by(date) %>% 
  summarise(number_orders = n(),
         yes = sum(yes),
         no = sum(no))

完整的代码可以用dplyr的管道操作写成一行

df1 <- df1 %>% mutate(yes = (check == 'yes'), no = (check == 'no')) %>%
  group_by(date) %>% 
  summarise(number_orders = n(),
         yes = sum(yes),
         no = sum(no))