简体   繁体   English

按区域然后按自定义组汇总事件

[英]Summarize occurrences by area and then by custom groups

I have below dataset that takes a 2 column dataset and creates age group categories depending on stated CustomerAge.我有以下数据集,它采用 2 列数据集并根据规定的 CustomerAge 创建年龄组类别。

    library(tidyverse)
    
    df <- 
      read.table(textConnection("Area   CustomerAge
    A 28 
    A 40
    A 70
    A 19
    B 13
    B 12
    B 72
    B 90"), header=TRUE)
    

df2 <- df %>% 
  mutate(
    # Create categories
    Customer_Age_Group = dplyr::case_when(
      CustomerAge <= 18            ~ "0-18",
      CustomerAge > 18 & CustomerAge <= 60 ~ "19-60",
      CustomerAge > 60             ~ ">60"
    ))

What I am looking to achieve is an output summary that looks like the below:我希望实现的是 output 摘要,如下所示:

Area区域 Customer_Age_Group客户_年龄_组 Occurrences出现次数
A一种 0-18 0-18岁 0 0
A一种 19-59 19-59 3 3个
A一种 >60 >60 1 1个
B 0-18 0-18岁 2 2个
B 19-59 19-59 0 0
B >60 >60 2 2个

To include also 0 occurences you need count() , ungroup() and complete() :要包括 0 次出现,您需要count()ungroup()complete()

df2 %>% group_by(Area, Customer_Age_Group,.drop = FALSE) %>% 
count() %>% 
ungroup() %>% 
complete(Area, Customer_Age_Group, fill=list(n=0))

This will show also 0 occurences.这也将显示 0 次出现。

To sort for Area and Age group:要按区域和年龄组排序:

df2 %>% group_by(Area, Customer_Age_Group,.drop = FALSE) %>% 
count() %>% 
ungroup() %>% 
complete(Area, Customer_Age_Group, fill=list(n=0)) %>% 
arrange(Area, parse_number(Customer_Age_Group))

group_by and summarise is what you're looking for. group_bysummarise是你要找的。

df2 %>% group_by(Area, Customer_Age_Group) %>% summarise(Occurences = n())

However note that this won't show categories with zero occurences in your data set.但是请注意,这不会显示数据集中出现次数为零的类别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM