簡體   English   中英

我如何使用R獲取數據表行中所有第n個CONSECUTIVE值的列名

[英]How do I get the column names for ALL the nth CONSECUTIVE occurrence of a value in a data table row using R

我正在尋找一種方法來確定數據框中客戶的“加入”,“流失”和“重新加入”日期(客戶作為行標簽,月/年作為列標簽,收入作為值),其中:

  • 客戶的收益值非零的第一個月=加入日期
  • 如果連續13個月沒有收入(零值),則將第13個月視為“流失”月份。
  • 如果在發生“客戶流失”月份之后,該客戶的值不為零,則該列中的相應月份為“重新加入”月份。
  • 如果在重新加入月份之后還有另外13個月沒有收入,則該13個零值列中的最后一個月份是另一個流失月份

我可能可以將其與For循環一起使用(請參見下面的偽代碼),但我想知道是否有一種更優雅的方法可以通過Apply函數或dplyr軟件包來實現。

因此,數據幀如下所示:

    > clients
                  Jan08 Feb08 Mar08 Apr08 May08 ... 
    client 1      1     0     0     0     0     ... 
    client 2      0     -6    6    -6     6     ... 
    client 3      28    4     108   93    3.85  ... 
                   ............

我正在尋找運行這樣的邏輯:

    For each client {
      For each month {
        If the value this month == 0 {
          If the previous 12 months values all == 0* {
            If there is a non-zero value exactly 13 months before {
              then mark the current month as a churn month
            }
          }
        }
        Else {
          If this is the first non zero value in the row {
            then mark the current month as the join month
          }
        Else {
          If this is the first non-zero value in 13 or more months {
            then mark this as a rejoin month
          }
        }
      }
    }

*注意:與客戶端2一樣,我可能會有負值,所以我不能僅求和值來確定進行中的值是否全部為零。

任何方向對此將不勝感激。

我認為解決此問題的最佳方法是將其更改為較長的數據幀,而不是較寬的數據幀。 例如,它可能看起來像這樣:

clients <- data.frame(
  client = c(rep(1, 15)),
  month = rep(1:15, 1),
  activity = c(1, rep(0, 13), 1)
)

看起來像這樣:

> clients
   client month activity
1       1     1        1
2       1     2        0
3       1     3        0
4       1     4        0
5       1     5        0
6       1     6        0
7       1     7        0
8       1     8        0
9       1     9        0
10      1    10        0
11      1    11        0
12      1    12        0
13      1    13        0
14      1    14        0
15      1    15        1

完成此操作后,您可以通過dplyr groupby方法分別對待每個客戶端。 剩下的就是利用窗口函數來處理您提供的邏輯。

為了使用窗口函數,我使用了library(zoo)rollapply方法。 我在這里的另一個解決方案中使用了這種方法,您可以在類似的問題中看到該方法。

從這里我們可以簡單地應用我們所追求的邏輯。 因此,一個極為混亂的版本可能如下所示:

clients %>% group_by(client) %>%
  mutate(
    isChurn = rollapply(activity, 13, function(x) {
      all(x == 0)
    }, align="right", fill=NA), # if there are 13 consecutive months with no earnings

    isRejoin = rollapply(activity, 14, function(x) {
      all(x[1:13] == 0) & x[14] != 0
    }, align="right", fill=NA)
  )

哪個具有基於my data.frame的輸出:

Source: local data frame [15 x 5]
Groups: client

   client month activity isChurn isRejoin
1       1     1        1      NA       NA
2       1     2        0      NA       NA
3       1     3        0      NA       NA
4       1     4        0      NA       NA
5       1     5        0      NA       NA
6       1     6        0      NA       NA
7       1     7        0      NA       NA
8       1     8        0      NA       NA
9       1     9        0      NA       NA
10      1    10        0      NA       NA
11      1    11        0      NA       NA
12      1    12        0      NA       NA
13      1    13        0   FALSE       NA
14      1    14        0    TRUE    FALSE
15      1    15        1   FALSE     TRUE

我不確定您是否只希望將流失月份標記為流失月份。 但是我認為這將使您到達那里,您應該能夠弄清楚其余的一切。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM