rsquaredacademy / rfm

Tools for Customer Segmentation using RFM Analysis
https://rfm.rsquaredacademy.com/
Other
58 stars 28 forks source link

Missing column error when handling customer level data #92

Open aravindhebbali opened 2 months ago

aravindhebbali commented 2 months ago

rfm_table_customer() returns an error in the absence of recency_days column in the customer level data. This happens due to hard coding the column name.

# load library
library(rfm)

# remove recency_days column
data <- rfm_data_customer[, -5]

# analysis date
analysis_date <- as.Date('2007-01-01')

# generate rfm score
rfm_table_customer(data, customer_id, number_of_orders,
most_recent_visit, revenue, analysis_date)
#> Error in `select()`:
#> ! Can't subset columns that don't exist.
#> ✖ Column `recency_days` doesn't exist.
#> Backtrace:
#>      ▆
#>   1. ├─rfm::rfm_table_customer(...)
#>   2. ├─rfm:::rfm_table_customer.default(...) at rfm/R/rfm-table-customer.R:50:23
#>   3. │ └─data %>% ... at rfm/R/rfm-table-customer.R:94:5
#>   4. ├─dplyr::select(...)
#>   5. ├─dplyr:::select.data.frame(...)
#>   6. │ └─tidyselect::eval_select(expr(c(...)), data = .data, error_call = error_call)
#>   7. │   └─tidyselect:::eval_select_impl(...)
#>   8. │     ├─tidyselect:::with_subscript_errors(...)
#>   9. │     │ └─rlang::try_fetch(...)
#>  10. │     │   └─base::withCallingHandlers(...)
#>  11. │     └─tidyselect:::vars_select_eval(...)
#>  12. │       └─tidyselect:::walk_data_tree(expr, data_mask, context_mask)
#>  13. │         └─tidyselect:::eval_c(expr, data_mask, context_mask)
#>  14. │           └─tidyselect:::reduce_sels(node, data_mask, context_mask, init = init)
#>  15. │             └─tidyselect:::walk_data_tree(new, data_mask, context_mask)
#>  16. │               └─tidyselect:::eval_bang(expr, data_mask, context_mask)
#>  17. │                 └─tidyselect:::walk_data_tree(expr[[2]], data_mask, context_mask)
#>  18. │                   └─tidyselect:::eval_c(expr, data_mask, context_mask)
#>  19. │                     └─tidyselect:::reduce_sels(node, data_mask, context_mask, init = init)
#>  20. │                       └─tidyselect:::walk_data_tree(new, data_mask, context_mask)
#>  21. │                         └─tidyselect:::as_indices_sel_impl(...)
#>  22. │                           └─tidyselect:::as_indices_impl(...)
#>  23. │                             └─tidyselect:::chr_as_locations(x, vars, call = call, arg = arg)
#>  24. │                               └─vctrs::vec_as_location(...)
#>  25. └─vctrs (local) `<fn>`()
#>  26.   └─vctrs:::stop_subscript_oob(...)
#>  27.     └─vctrs:::stop_subscript(...)
#>  28.       └─rlang::abort(...)

Created on 2024-06-14 with reprex v2.0.2

aravindhebbali commented 2 months ago
# load library
library(rfm)

# remove recency_days column
data <- rfm_data_customer[, -5]

# analysis date
analysis_date <- as.Date('2007-01-01')

# generate rfm score
score <- 
  rfm_table_customer(data, customer_id, number_of_orders,
                     most_recent_visit, revenue, analysis_date)
head(score$rfm)
#>   customer_id recency_days transaction_count amount recency_score
#> 1       22086          232                 9    777             2
#> 2        2290          115                16   1555             4
#> 3       26377           43                 5    336             5
#> 4       24650           64                12   1189             5
#> 5       12883           23                12   1229             5
#> 6        2119           72                11    929             5
#>   frequency_score monetary_score rfm_score first_name last_name
#> 1               2              2       222  Maddalena      Erie
#> 2               5              5       455    Bradley    Sesser
#> 3               1              1       511    Gwenora     Asser
#> 4               4              4       544   Hendrick      Josh
#> 5               4              5       545   Cathleen   Musterd
#> 6               4              3       543     Norrie     Brear
#>                    email
#> 1          merie0@go.com
#> 2      bsesser1@time.com
#> 3      gasser2@issuu.com
#> 4          hjosh3@ed.gov
#> 5    cmusterd4@hc360.com
#> 6 nbrear5@techcrunch.com

Created on 2024-06-14 with reprex v2.0.2