I'm trying to calculate retention rates of customers in my platform. I want to see how many customers came to the platform 4 times in 4 weeks, 3 times in 4 weeks, twice in 4 weeks, and once in 4 weeks.
The first and last data points are easy to get, but I can't figure out how to calculate the middle two.
I have array's of customer ID's that were active per week. For example:
week_one = [1, 3, 4, 5, 7]
week_two = [1, 2, 3, 6]
week_three = [1, 2, 7]
week_four = [1, 2, 3, 8]
The result I'm trying to get is:
customers who used the platform 4 times in 4 weeks: [1]
customers who used the platform 3 times in 4 weeks: [2, 3]
customers who used the platform 2 times in 4 weeks: [7]
customers who used the platform 1 time in 4 weeks: [4, 5, 6, 8]
...
With these values, I can then say:
1 customer used the platform 4 weeks in a row, 2 customers used the platform 3 times in the 4 week period, 1 customer used the platform 2 times in the 4 week period, and 4 customers used the platform 1 time in the four week period.
Any help would be appreciated.
CodePudding user response:
My approach to this would be to join all the arrays into one bigger array
monthly_visits = week_one week_two week_three week_four
visit_count = {}
# from array of unique values, count how many times each value occurs
monthly_visits.uniq.each do |visitor_id|
visit_count[visitor_id] = monthly_visits.count(visitor_id)
end
visit_count
# Hash returns each visitor_id as key, and number of visits as the value
# => {1=>4, 3=>3, 4=>1, 5=>1, 7=>2, 2=>3, 6=>1, 8=>1}
Hope this is the solution for your problem.
CodePudding user response:
Enumerable#tally could be used here:
[week_one, week_two, week_three, week_four].flatten.tally
#=> {1=>4, 3=>3, 4=>1, 5=>1, 7=>2, 2=>3, 6=>1, 8=>1}
