[英]How to create a funnel view in R
I have a dataframe that has the following structure.我有一个具有以下结构的 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
I need to create a funnel view by utilizing the data frame considering the following conditions:我需要考虑以下条件,利用数据框创建一个漏斗视图:
May-21, the month should be auto-populating based on Date. 5 月 21 日,应根据日期自动填充月份。 If there are records of multiple months then it should be sequenced by month in descending month order.
如果有多个月份的记录,则应按月份降序排列。 Here,
Total
is count of Total unique ProdID for that particular month.在这里,
Total
是该特定月份的 Total 唯一 ProdID 计数。 Regular
is the count and sum of those ProdID having class as "Regular" and similar logic for the "Premium" row. Regular
是将 class 作为“Regular”以及“Premium”行的类似逻辑的 ProdID 的计数和总和。 The miscellaneous
row is a count of ProdID having classes other than "Regular" and "Premium". miscellaneous
行是具有“常规”和“高级”以外的类别的 ProdID 的计数。 RS (Premium)
is count and sum of those where the class is "Premium" along with set as "RS". 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%
On StackOverflow it is expected that you show your own attempts to solve the problem.在 StackOverflow 上,您应该展示自己解决问题的尝试。 Having said that, this should get you started:
话虽如此,这应该让你开始:
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.