I would need your advice as I am struggling to find out the right command in R.
Basically I would like to calculate the retention rate for the specific customers. The customer_math is the snapshot of when the customer was active, which includes a time range of 8 years.
customer customer_math
Apple 1
Tesco 10
Nespresso 1001
Dell 11
BMW 11111100
The final dataset should look like this:
customer customer_math retention_rate
Apple 1 1
Tesco 10 0.5
Nespresso 1001 0.5
Dell 11 1
BMW 11111100 0.75
Any ideas of how I can solve my problem?
Your help is very appreciated! Thanks!
library(tidyverse)
tribble(
~customer, ~customer_math,
"Apple", 1,
"Tesco", 10,
"Nespresso", 1001,
"Dell", 11,
"BMW", 11111100
) %>%
mutate(active_count = str_count(customer_math, "1"),
periods = str_length(customer_math),
retention_rate = active_count / periods)
## A tibble: 5 x 5
# customer customer_math active_count periods retention_rate
# <chr> <dbl> <int> <int> <dbl>
#1 Apple 1 1 1 1
#2 Tesco 10 1 2 0.5
#3 Nespresso 1001 2 4 0.5
#4 Dell 11 2 2 1
#5 BMW 11111100 6 8 0.75