Search code examples
rchurn

Calculate churn in R given data variables


I'm trying to calculate the number of retained students using R. The two variables I'm working with are 'registration_date' (mm/dd/yr) and 'date_of_last_login' (mm/dd/yr). A student is considered retained if they logged-in in the preceding 30 days.

ID                    1 ,      2,        3,         4,          5
registration_date    2/1/15, 2/1/15,  3/15/15,   2/10/15,    4/15/15
date_of_last_login   2/3/15, 3/15/15, 4/30/15,   4/25/15,    5/16/15

I imagine the idea is to create a new variable: 'retained students' but I am not sure how to set up the formula in R.


Solution

  • Assuming you mean the 30 days previous to today:

    last_login <- c("2/3/15","3/15/15","4/30/15")
    login <- as.Date(last_login, format = '%m/%d/%y')
    
    retained_students <- (Sys.Date()-login < 30)
    retained_students
    

    retained_students is then a vector with either TRUE or FALSE for each login