使用RFM R软件包,有没有一种方法可以限制transaction_count以仅计算从分析日期算起的上一年的交易?
https://github.com/rsquaredacademy/rfm/blob/master/R/rfm-table-transaction.R https://github.com/rsquaredacademy/rfm
result <-
data %>%
dplyr::select(!! cust_id, !! odate, !! revenues) %>%
dplyr::group_by(!! cust_id) %>%
dplyr::summarise(
date_most_recent = max(!! odate), amount = sum(!! revenues),
transaction_count = dplyr::n()
) %>%
dplyr::mutate(
recency_days = (analysis_date - date_most_recent) / lubridate::ddays()
) %>%
dplyr::select(
!! cust_id, date_most_recent, recency_days, transaction_count,
amount
) %>%
magrittr::set_names(c("customer_id", "date_most_recent", "recency_days", "transaction_count", "amount"))
我的数据集如下:
customer_id date amount
111 21/03/2019 10.50
111 10/11/2018 9.50
111 05/06/2017 50.70
我想确保transaction_count只回溯到分析日期之后的一年。例如,如果分析日期为22/03/2019,则交易计数为2。
答案 0 :(得分:0)
澄清您的笔记-“仅计算分析日期以来的上一年的交易吗?”,假设您希望限制开始日期(21/03/2018)和分析日期(22/03/2019)之间的交易计数在您的示例中,如果您有其他要求,请在注释中添加注释。我建议您限制输入数据本身中的记录,而不是考虑在您提出的问题RFM package code中修改记录,这是一个R脚本示例,供您在下面参考。
#Define analysis window, ******before passing data to RFM package****
#Referring 365 days to catch up last one year period
analysis_period_days <- 365
#assign static value for analysis_date
#Or you can go with analysis_date <- Sys.Date()
#Or you also can give as a parameter if you have requirement to use past date as analysis date.
analysis_start_date <- analysis_date - analysis_period_days
analysis_end_date <- analysis_date
#Use analysis_start_date and analysis_end_date for filtering the records while fetching from DB,
#Or directly apply in your DataFrame before calling RFM method
####RFM pcakge call here#######