【问题标题】:generate a new variable based on the aging of another variable根据另一个变量的老化生成一个新变量
【发布时间】:2020-05-24 01:54:57
【问题描述】:

我有一个如下所示的数据集

ID. Invoice. Date of Invoice.  paid or not.  

1    1         10/31/2019       yes
1    1         10/31/2019       yes
1    2         11/30/2019       no
1    3         12/31/2019       no

2    1         09/30/2019       no
2    2         10/30/2019       no
2    3         11/30/2019       yes

3    1         7/31/2019        no
3    2         9/30/2019        yes
3    3         12/31/2019       no

4    1         7/31/2019        yes
4    2         9/30/2019        no
4    3         12/31/2019       yes

我想知道客户是否愿意付款。客户只要补了新的发票,旧的发票没付,我会给他一个好分数。所以对于客户 1 和 3,我给了“好”,客户 2 是“坏”的分数。

所以最终数据将多出一列,其值分别为好和坏。

身份证。发票。发票日期。支付与否。好与坏

1    1         10/31/2019       yes          bad
1    1         10/31/2019       yes          bad
1    2         11/30/2019       no           bad
1    3         12/31/2019       no           bad

2    1         09/30/2019       no           good
2    2         10/30/2019       no           good
2    3         11/30/2019       yes          good

3    1         7/31/2019        no           good
3    2         9/30/2019        yes          good
3    3         12/31/2019       no           good

4    1         7/31/2019        yes          good
4    2         9/30/2019        no           good
4    3         12/31/2019       yes          good

【问题讨论】:

  • 不错不错。 XD 我已经编辑了它们。
  • 谢谢,关于逻辑,你是如何分类的。我不清楚
  • 这里是所有客户的所有发票。只要支付了新发票而旧发票仍未支付,我会假设他仍然愿意支付而不是完全忽略发票。
  • 对于 'ID', 3,'yes' 是在 2019 年 9 月 30 日,而下一张发票没有支付

标签: r dplyr gdata


【解决方案1】:

假设您的 Date of Invoice. 已经订购,那么这里是使用 ave 的基本 R 解决方案

df$`good or band.` <- ave(df$`paid or not.`,df$ID., FUN = function(v) ifelse(which(v=="yes")==1,"bad","good"))

这样

> df
  ID. Invoice. Date of Invoice. paid or not. good or band.
1   1        1       09/30/2019           no          good
2   1        2       10/30/2019           no          good
3   1        3       11/30/2019          yes          good
4   2        1       10/31/2019          yes           bad
5   2        2       11/30/2019           no           bad
6   2        3       12/31/2019           no           bad
7   3        1        7/31/2019           no          good
8   3        2        9/30/2019          yes          good
9   3        3       12/31/2019           no          good

数据

df <- structure(list(ID. = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), Invoice. = c(1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), `Date of Invoice.` = c("09/30/2019", 
"10/30/2019", "11/30/2019", "10/31/2019", "11/30/2019", "12/31/2019", 
"7/31/2019", "9/30/2019", "12/31/2019"), `paid or not.` = c("no", 
"no", "yes", "yes", "no", "no", "no", "yes", "no")), class = "data.frame", row.names = c(NA, 
-9L))

【讨论】:

    【解决方案2】:

    逻辑不清楚。可能,我们可以在除第一行之外的任何行中检查“是”,然后按“ID”分组

    library(dplyr)
    library(lubridate)
    df1 %>% 
       mutate(Date_of_Invoice = mdy(Date_of_Invoice)) %>% 
       arrange(ID, Date_of_Invoice) %>%
       group_by(ID) %>%
       mutate(flag = c('bad', 'good')[1 + any(paid_or_not[-1] == "yes")])
    # A tibble: 9 x 5
    # Groups:   ID [3]
    #     ID Invoice Date_of_Invoice paid_or_not flag 
    #  <int>   <int> <date>          <chr>       <chr>
    #1     1       1 2019-09-30      no          good 
    #2     1       2 2019-10-30      no          good 
    #3     1       3 2019-11-30      yes         good 
    #4     2       1 2019-10-31      yes         bad  
    #5     2       2 2019-11-30      no          bad  
    #6     2       3 2019-12-31      no          bad  
    #7     3       1 2019-07-31      no          good 
    #8     3       2 2019-09-30      yes         good 
    #9     3       3 2019-12-31      no          good 
    

    数据

    df1 <- structure(list(ID = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), Invoice = c(1L, 
    2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), Date_of_Invoice = c("09/30/2019", 
    "10/30/2019", "11/30/2019", "10/31/2019", "11/30/2019", "12/31/2019", 
    "7/31/2019", "9/30/2019", "12/31/2019"), paid_or_not = c("no", 
    "no", "yes", "yes", "no", "no", "no", "yes", "no")), class = "data.frame", row.names = c(NA, 
    -9L))
    

    【讨论】:

    • 哼,如果客户最早的发票被支付了,那么代码就不再起作用了。
    • @Yufang 虽然不清楚。如果您可以使用新示例更新您的帖子,将会很有帮助
    • 我再添加一个例子。客户都有已付和未付的发票。如果客户有 Step 的已付发票和 8 月的未付发票,我会假设该客户没有支付 8 月的发票,因为他认为金额已关闭或其他原因。所以我仍然把他列为好客户。
    • @Yufang 我猜你发布了一个新问题。这是这个的更新吗
    • 另一方面,如果客户从年初到八月支付了发票,但从八月到现在还没有支付。我暂停他永远不会再付款。所以我把他列为坏客户。如果客户有不按时付款的习惯,那么用一月发票付款,二月发票未付款,八月发票再次付款。我也认为他是一个很好的客户。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多