簡體   English   中英

如何在 R 中創建漏斗視圖

[英]How to create a funnel view in R

我有一個具有以下結構的 dataframe。

ProdID         Date                    class          price        set
PD-10011       2021-05-01 10:12:16     Regular        1000         ZR
PD-10712       2021-05-02 18:12:06     Premium        1000         ZR
PD-10213       2021-05-02 16:02:59     Premium        1000         ZR
PD-10814       2021-05-03 17:12:06     Premium        1000         RS
PD-10315       2021-05-04 19:10:11     Other          1000         RR
PD-10616       2021-05-04 13:18:14     Expired        1000         ZR
PD-10617       2021-05-04 15:14:19     Regular        1000         ZR

我需要考慮以下條件,利用數據框創建一個漏斗視圖:

5 月 21 日,應根據日期自動填充月份。 如果有多個月份的記錄,則應按月份降序排列。 在這里, Total是該特定月份的 Total 唯一 ProdID 計數。 Regular是將 class 作為“Regular”以及“Premium”行的類似邏輯的 ProdID 的計數和總和。 miscellaneous行是具有“常規”和“高級”以外的類別的 ProdID 的計數。 RS (Premium)是 class 為“Premium”且設置為“RS”的那些的計數和總和。

May-21           Count of ProdID       Percentage Count       Sum of Price        Percentage Sum
Total            7                     100.00%                7000                100.00%
Regular          2                     28.57%                 2000                28.57%
Premium          3                     42.85%                 3000                42.85%
miscellaneous    2                     28.57%                 2000                28.57%
RS (Premium)     1                     33.33%                 1000                33.33%  

         

在 StackOverflow 上,您應該展示自己解決問題的嘗試。 話雖如此,這應該讓你開始:

library(tidyverse)
library(lubridate)
library(janitor)

df <- tibble::tribble(
                                      ~ProdID,                 ~Date,    ~class, ~price, ~set,
                                   "PD-10011", "2021-05-01 10:12:16", "Regular",  1000L, "ZR",
                                   "PD-10712", "2021-05-02 18:12:06", "Premium",  1000L, "ZR",
                                   "PD-10213", "2021-05-02 16:02:59", "Premium",  1000L, "ZR",
                                   "PD-10814", "2021-05-03 17:12:06", "Premium",  1000L, "RS",
                                   "PD-10315", "2021-05-04 19:10:11",   "Other",  1000L, "RR",
                                   "PD-10616", "2021-05-04 13:18:14", "Expired",  1000L, "ZR",
                                   "PD-10617", "2021-05-04 15:14:19", "Regular",  1000L, "ZR"
                                   )
df %>%
  mutate(Date = ymd_hms(Date)) %>% 
  filter(Date %within% interval("2021-05-01", "2021-05-31")) %>%
  mutate(class = case_when(class == "Premium" & set == "RS" ~ "RS (Premium)",
                           class != "Premium" & class != "Regular" ~ "miscellaneous",
                           class == "Premium" ~ "Premium",
                           class == "Regular" ~ "Regular")) %>% 
  group_by(class, price) %>% 
  summarise(count_of_prod_ID = n()) %>% 
  ungroup() %>% 
  mutate(`Sum of Price` = count_of_prod_ID * price,
         `Count (%)` = round((count_of_prod_ID / 7)*100, 2)) %>%
  janitor::adorn_totals() %>% 
  select(-c(price)) %>% 
  arrange(desc(count_of_prod_ID))
#>        class count_of_prod_ID Sum of Price Count (%)
#>         Total                7         7000    100.00
#> miscellaneous                2         2000     28.57
#>       Premium                2         2000     28.57
#>       Regular                2         2000     28.57
#>  RS (Premium)                1         1000     14.29

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM