0

I would like to use levenshtein distance to calculate the behaviour of a customer and in which status the customer was in each year concatenated with the previous status.

In Tableau, I have calculated the following column to display in each year the customer can be: (Not a customer (Not), new Customer (New), Same revenue (Same), Less Revenue (Down), More Revenue (UP) and be Lapsed (Lapsed).

Now I would like to display a customer for instance in year 2019 and see his history, like this: NOT-NEW-UP-UP-SAME-SAME-DOWN and extract a customer table of 2019 year customers to calculate levenshtein.

Costomer - Year - Customer State
Apple      2012   NOT
Apple      2013   NOT
Apple      2014   NEW
Apple      2015   UP
Apple      2016   DOWN
Apple      2017   LAPSED
Apple      2018   RETURN
Costomer - Year - Customer State - Cust. concatenated
Apple      2012   NOT         NOT
Apple      2013   NOT         NOT-NOT
Apple      2014   NEW         NOT-NOT-NEW
Apple      2015   UP          ..
Apple      2016   DOWN        ..
Apple      2017   LAPSED      ..
Apple      2018   RETURN      NOT-NOT-NEW-UP-DOWN-LAPSED-RETURN

Many Thanks for your help!

Lebowski
  • 51
  • 6

1 Answers1

1

You can use Reduce with argument accumulate = TRUE, i.e.

Reduce(paste, df$Customer_State, accumulate = TRUE)
#[1] "NOT"    "NOT NOT"  "NOT NOT NEW"  "NOT NOT NEW UP"   "NOT NOT NEW UP DOWN"   "NOT NOT NEW UP DOWN LAPSED"        "NOT NOT NEW UP DOWN LAPSED RETURN"
Sotos
  • 51,121
  • 6
  • 32
  • 66