我使用了商业数据。 RFM详细信息Tools for RFM (recency, frequency and monetary) analysis.但我没有在链接中使用数据。我只想应用此包装。尽管我固定了日期类型,但我不能使用它。
#clensing data
> library(dplyr)
> data <- fread("RFMdata.csv")
> str(data)
Classes ‘data.table’ and 'data.frame': 541909 obs. of 8 variables:
$ InvoiceNo : chr "536365" "536365" "536365" "536365" ...
$ StockCode : chr "85123A" "71053" "84406B" "84029G" ...
$ Description: chr "WHITE HANGING HEART T-LIGHT HOLDER" "WHITE METAL
LANTERN" "CREAM CUPID HEARTS COAT HANGER" "KNITTED UNION FLAG HOT WATER
BOTTLE" ...
$ Quantity : int 6 6 8 6 6 2 6 6 6 32 ...
$ InvoiceDate: chr "12/1/2010 8:26" "12/1/2010 8:26" "12/1/2010 8:26"
"12/1/2010 8:26" ...
$ UnitPrice : num 2.55 3.39 2.75 3.39 3.39 7.65 4.25 1.85 1.85 1.69 ...
$ CustomerID : int 17850 17850 17850 17850 17850 17850 17850 17850 17850
13047 ...
$ Country : chr "United Kingdom" "United Kingdom" "United Kingdom"
"United Kingdom" ...
- attr(*, ".internal.selfref")=<externalptr>
> data <- data %>%
mutate(InvoiceNo=as.factor(InvoiceNo), StockCode=as.factor(StockCode),
InvoiceDate=as.Date(InvoiceDate, '%m/%d/%Y %H:%M'), CustomerID=as.factor(CustomerID),
Country=as.factor(Country))
> data <- data %>%
mutate(total_dolar = Quantity*UnitPrice)
> str(data)
'data.frame': 541909 obs. of 8 variables:
$ InvoiceNo : Factor w/ 25900 levels "536365","536366",..: 1 1 1 1 1 1 1 2
2 3 ...
$ StockCode : Factor w/ 4070 levels "10002","10080",..: 3538 2795 3045
2986 2985 1663 801 1548 1547 3306 ...
$ Description: chr "WHITE HANGING HEART T-LIGHT HOLDER" "WHITE METAL
LANTERN" "CREAM CUPID HEARTS COAT HANGER" "KNITTED UNION FLAG HOT WATER
BOTTLE" ...
$ Quantity : int 6 6 8 6 6 2 6 6 6 32 ...
$ InvoiceDate: Date, format: "2010-12-01" "2010-12-01" "2010-12-01" "2010-
12-01" ...
$ UnitPrice : num 2.55 3.39 2.75 3.39 3.39 7.65 4.25 1.85 1.85 1.69 ...
$ CustomerID : Factor w/ 4372 levels "12346","12347",..: 4049 4049 4049
4049 4049 4049 4049 4049 4049 541 ...
$ Country : Factor w/ 38 levels "Australia","Austria",..: 36 36 36 36 36
36 36 36 36 36 ...
$ total_dolar: num 15.3 20.3 22 20.3 20.3 ...
#RFM act
> library(rfm)
> rfm_r <- rfm_table_order(data = data, customer_id = CustomerID, order_date = InvoiceDate, revenue = total_dolar)
有关“日期”的错误消息,我该如何解决?
Error in mutate_impl(.data, dots) :
Evaluation error: can only subtract from "Date" objects.