【问题标题】:Creating a retention cohort by date of acquistion in R在 R 中按获取日期创建保留队列
【发布时间】:2015-12-02 07:49:42
【问题描述】:

我是 R 新手,已经在线学习了几个教程并付费,但仍然在努力实现我的要求。我想在 R 中建立一个保留队列。我目前在 excel 中执行此操作,每次我需要实施一个时都需要 4-5 个小时。因此,探索看看 R 是否可以提供帮助。看起来是这样,但需要一些方向。

下面是excel中的输出。如果您注意到 2011 年 5 月,我有 31 位客户加入,他们的进展按月计算,直到当月。

Output in excel

This is the input variable

初始列是客户 ID、加入日期和到期日期。另外两列将djde 转换为文本。 K 之后的列正在处理数据以检查客户在给定月份是否活跃或是否流失?我在文本中使用加入日期来表示活动,并将其附加到“C”以表示到期。稍后我只计算具有日期的列以获取同类群组。

那么,我如何在 R 中实现它。

假设这是样本数据,我需要从 2015 年 5 月 1 日到 2016 年 1 月 1 日的同期群

customer dj         exp
abc      01/05/15   25/6/15
efg      01/05/15   25/7/15
ghd      01/05/15   25/7/15
mkd      01/06/15   25/7/15
kskm     01/06/15   05/8/15

这是我想根据上述数据创建的。

Cohort      M0      M1      M2      M3    M4  
2015-05     3       3       2       0     0
2015-06     2       2       1       1     0

说明:M0 为加入之日起的月份。所以 2015 年 5 月有 3 人加入我们,他们都在 5 月活跃。 M1 将是 6 月,所有这些都在 6 月活跃。我们在 6 月底 25 日失去了 1 位客户,因此会认为他在 6 月活跃,但在 M2 中,我的计数从 3 下降到 2。5 月加入客户的 M3 对应于我们失去客户的 8 月份.

2015-06 年队列的类似过程。 M1 表示 7 月,M3 表示 9 月。

【问题讨论】:

    标签: r


    【解决方案1】:

    修改您的代码如下,这工作谢谢!现在想办法让 M0 到 M(n) 动态化。

    library(readxl)
    library(zoo)
    library(plyr)
    
    # Read in the data
    df <- read.csv("~/Desktop/R/data.csv")
    df$dj <- as.Date(df$dj,"%d/%m/%y")
    df$exp <- as.Date(df$exp,"%d/%m/%y")
    
    # The data in the file has different variable names than your example data
    # so I'm changing them to match
    names(df)[1:3] <- c("customer","dj","exp")
    
    # Make a variable called Cohort that contains only the year and month of joining
    # as.yearmon() comes from the 'zoo' package
    df$Cohort <- as.yearmon(df$dj)
    
    # Calculate the difference in months between date of expiry and date of joining
    df$MonthDiff <- ceiling((df$exp-df$dj)/30)
    #df$MonthDiff <- 12*(as.yearmon(df$exp+months(1))-df$Cohort)
    
    # Use ddply() from the 'plyr' package to get the frequency of subjects that are
    # still active after 0, 1, 2, 3, and 4 months.
    df1 <- ddply(df,.(Cohort),summarize,
                 M0 = sum(MonthDiff > 0),
                 M1 = sum(MonthDiff > 1),
                 M2 = sum(MonthDiff > 2),
                 M3 = sum(MonthDiff > 3),
                 M4 = sum(MonthDiff > 4),
                 M5 = sum(MonthDiff > 5)
                 )
    df1
    
    df1
        Cohort M0 M1 M2 M3 M4 M5
    1 May 2015  3  3  2  0  0  0
    2 Jun 2015  2  2  1  0  0  0
    

    现在

    【讨论】:

      【解决方案2】:

      试试这个:

      library(readxl)
      library(zoo)
      library(plyr)
      
      # Read in the data
      df <- read_excel("MyFile.xlsx")
      
      # The data in the file has different variable names than your example data
      # so I'm changing them to match
      names(df)[1:3] <- c("customer","dj","exp")
      
      # Make a variable called Cohort that contains only the year and month of joining
      # as.yearmon() comes from the 'zoo' package
      df$Cohort <- as.yearmon(df$dj)
      
      # Calculate the difference in months between date of expiry and date of joining
      df$MonthDiff <- 12*(as.yearmon(df$exp)-df$Cohort)
      
      # Use ddply() from the 'plyr' package to get the frequency of subjects that are
      # still active after 0, 1, 2, 3, and 4 months.
      df1 <- ddply(df,.(Cohort),summarize,
                   M0 = sum(MonthDiff >= 0),
                   M1 = sum(MonthDiff >= 1),
                   M2 = sum(MonthDiff >= 2),
                   M3 = sum(MonthDiff >= 3),
                   M4 = sum(MonthDiff >= 4))
      
      df1
      #   Cohort M0 M1 M2 M3 M4
      # May 2015  3  3  2  0  0
      # Jun 2015  2  1  0  0  0
      

      这假设当您从 Excel 中读取数据时,日期被格式化为日期。如果不是,您可以使用以下内容:

      df$dj <- as.Date(df$dj,"%d/%m/%y")
      df$exp <- as.Date(df$exp,"%d/%m/%y")
      

      【讨论】:

      • 嗨,山姆,感谢您的帖子,并对造成的混乱感到抱歉。我已经把描述写得更清楚了。让我知道这是否有帮助。
      • 谢谢。有没有办法使分析月份 M0、M1、M2 等动态化?我试图通过从最小 datejoined 中减去最大到期时间然后将结果除以 30 来获得我需要的列数。
      • 嗨,Sam,六月队列应该是 2 2 1 1 0 而不是 2 1 0 0 0
      • 嗨,Sam,您能帮忙动态创建 M0 到 M5 列吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      • 2018-11-19
      • 2011-06-13
      • 2020-02-29
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      相关资源
      最近更新 更多