软件包“ rfm”中的rfm_table_order函数

时间:2018-07-10 16:07:35

标签: r

我在下面收到错误消息运行代码。感谢有人可以提供帮助。我认为问题出在日期转换上。谢谢!!

> # data structure
> str(bmdata)
'data.frame':   1094964 obs. of  3 variables:
 $ customer_ID: num  1 1 1 1 1 1 1 1 1 1 ...
 $ sales_date : Factor w/ 366 levels "1/1/2017 0:00:00",..: 86 66 81 82 84 85 105 116 122 124 ...
 $ sales      : num  182 120 91 63 58 56 251 24 269 113 ...
> head(bmdata,5)

  customer_ID         sales_date sales
1           1  11/3/2016 0:00:00   182
2           1 11/11/2016 0:00:00   120
3           1 11/25/2016 0:00:00    91
4           1 11/26/2016 0:00:00    63
5           1 11/28/2016 0:00:00    58
> # convert date from factor to date format
> bmdata[,2] <- as.Date(as.character(bmdata[,2]), format = "%m/%d/%Y") 
> # rfm analysis 
> rfm_result <- rfm_table_order(bmdata, customer_ID, sales_date, sales)
Error in mutate_impl(.data, dots) : 
  Evaluation error: can only subtract from "Date" objects.

1 个答案:

答案 0 :(得分:1)

问题是您没有给日期减去减去最近交易的数据即可获得新近度。试试这个:

 # some fake data
 set.seed(12)
 customer_ID <- sample(1:500, 500, rep = TRUE)
 sales_date <- sample(seq(as.Date('2016/01/01'), as.Date('2017/12/01'), by="day"), 500)
 sales <- sample(20:400, 500, rep = TRUE)

 bmdata <- data.frame (customer_ID,sales_date,sales)
 head(bmdata)
       customer_ID sales_date sales
1          35 2016-08-15    43
2         409 2016-09-02    79
3         472 2016-11-20   327
4         135 2016-05-08   191
5          85 2016-12-25   217
6          17 2017-05-22   242

library(rfm)

rfm_result <- rfm_table_order(data = bmdata,
                              customer_id = customer_ID,
                              order_date = sales_date,
                              revenue = sales,
                              # here the date, for example, Sys.Date()
                              analysis_date = Sys.Date())