Skip to contents

Recency, frequency, monetary and RFM score.

Usage

rfm_table_order(
  data = NULL,
  customer_id = NULL,
  order_date = NULL,
  revenue = NULL,
  analysis_date = NULL,
  recency_bins = 5,
  frequency_bins = 5,
  monetary_bins = 5,
  ...
)

Arguments

data

A data.frame or tibble.

customer_id

Unique id of the customer.

order_date

Date of the transaction.

revenue

Revenue from the customer.

analysis_date

Date of analysis.

recency_bins

Number of bins for recency or custom threshold.

frequency_bins

Number of bins for frequency or custom threshold.

monetary_bins

Number of bins for monetary or custom threshold.

...

Other arguments.

Value

rfm_table_order returns a list with the following:

rfm

RFM table.

analysis_date

Date of analysis.

frequency_bins

Number of bins used for frequency score.

recency_bins

Number of bins used for recency score.

monetary_bins

Number of bins used for monetary score.

threshold

thresholds used for generating RFM scores.

Examples

analysis_date <- as.Date("2006-12-31")
rfm_table_order(
  rfm_data_orders, customer_id, order_date, revenue,
  analysis_date
)
#> $rfm
#> # A tibble: 995 x 8
#>    customer_id        recency_days transaction_count amount recency_score
#>    <chr>                     <dbl>             <int>  <dbl>         <int>
#>  1 Abbey O'Reilly DVM          205                 6    472             3
#>  2 Add Senger                  140                 3    340             4
#>  3 Aden Lesch Sr.              194                 4    405             3
#>  4 Admiral Senger              132                 5    448             4
#>  5 Agness O'Keefe               90                 9    843             5
#>  6 Aileen Barton                84                 9    763             5
#>  7 Ailene Hermann              281                 8    699             3
#>  8 Aiyanna Bruen PhD           246                 4    157             3
#>  9 Ala Schmidt DDS             349                 3    363             2
#> 10 Alannah Borer               619                 4    196             1
#> # i 985 more rows
#> # i 3 more variables: frequency_score <int>, monetary_score <int>,
#> #   rfm_score <dbl>
#> 
#> $analysis_date
#> [1] "2006-12-31"
#> 
#> $frequency_bins
#> [1] 5
#> 
#> $recency_bins
#> [1] 5
#> 
#> $monetary_bins
#> [1] 5
#> 
#> $threshold
#>   recency_lower recency_upper frequency_lower frequency_upper monetary_lower
#> 1           1.0         115.0               1               4           12.0
#> 2         115.0         181.0               4               5          255.8
#> 3         181.0         297.4               5               6          382.0
#> 4         297.4         482.0               6               8          506.4
#> 5         482.0         977.0               8              15          666.0
#>   monetary_upper
#> 1          255.8
#> 2          382.0
#> 3          506.4
#> 4          666.0
#> 5         1489.0
#> 

# access rfm table
result <- rfm_table_order(
  rfm_data_orders, customer_id, order_date,
  revenue, analysis_date
)
result$rfm
#> # A tibble: 995 x 8
#>    customer_id        recency_days transaction_count amount recency_score
#>    <chr>                     <dbl>             <int>  <dbl>         <int>
#>  1 Abbey O'Reilly DVM          205                 6    472             3
#>  2 Add Senger                  140                 3    340             4
#>  3 Aden Lesch Sr.              194                 4    405             3
#>  4 Admiral Senger              132                 5    448             4
#>  5 Agness O'Keefe               90                 9    843             5
#>  6 Aileen Barton                84                 9    763             5
#>  7 Ailene Hermann              281                 8    699             3
#>  8 Aiyanna Bruen PhD           246                 4    157             3
#>  9 Ala Schmidt DDS             349                 3    363             2
#> 10 Alannah Borer               619                 4    196             1
#> # i 985 more rows
#> # i 3 more variables: frequency_score <int>, monetary_score <int>,
#> #   rfm_score <dbl>

# using custom threshold
rfm_table_order(rfm_data_orders, customer_id, order_date, revenue,
  analysis_date,
  recency_bins = c(115, 181, 297, 482), frequency_bins = c(4, 5, 6, 8),
  monetary_bins = c(256, 382, 506, 666)
)
#> $rfm
#> # A tibble: 995 x 8
#>    customer_id        recency_days transaction_count amount recency_score
#>    <chr>                     <dbl>             <int>  <dbl>         <int>
#>  1 Abbey O'Reilly DVM          205                 6    472             3
#>  2 Add Senger                  140                 3    340             4
#>  3 Aden Lesch Sr.              194                 4    405             3
#>  4 Admiral Senger              132                 5    448             4
#>  5 Agness O'Keefe               90                 9    843             5
#>  6 Aileen Barton                84                 9    763             5
#>  7 Ailene Hermann              281                 8    699             3
#>  8 Aiyanna Bruen PhD           246                 4    157             3
#>  9 Ala Schmidt DDS             349                 3    363             2
#> 10 Alannah Borer               619                 4    196             1
#> # i 985 more rows
#> # i 3 more variables: frequency_score <int>, monetary_score <int>,
#> #   rfm_score <dbl>
#> 
#> $analysis_date
#> [1] "2006-12-31"
#> 
#> $frequency_bins
#> [1] 4 5 6 8
#> 
#> $recency_bins
#> [1] 115 181 297 482
#> 
#> $monetary_bins
#> [1] 256 382 506 666
#> 
#> $threshold
#>   recency_lower recency_upper frequency_lower frequency_upper monetary_lower
#> 1             1           115               1               4             12
#> 2           115           181               4               5            256
#> 3           181           297               5               6            382
#> 4           297           482               6               8            506
#> 5           482           977               8              15            666
#>   monetary_upper
#> 1            256
#> 2            382
#> 3            506
#> 4            666
#> 5           1489
#>