根据分类变量分配日期

时间:2018-09-19 13:37:54

标签: r

我有一组Payment Date,格式如下:

ID  Payment Date   
1   18-01-01
1   18-02-03
2   18-04-03
2   18-05-08
2   18-06-06
3   17-12-23
3   18-01-22
3   18-02-24
4   17-11-09
4   18-12-06

我想添加一列Activation Date作为每个ID的最早付款日期,例如:

ID  Payment Date   Activation Date
1   18-01-01       18-01-01
1   18-02-03       18-01-01
2   18-04-03       18-04-03
2   18-05-08       18-04-03
2   18-06-06       18-04-03
3   17-12-23       17-12-23
3   18-01-22       17-12-23
3   18-02-24       17-12-23
4   17-11-09       17-11-09
4   18-12-06       17-11-09

我想知道,与其进入一个循环中,一个接一个地处理每个ID,不如要有一个更聪明的方法来做到这一点。

3 个答案:

答案 0 :(得分:1)

df = read.table(text = "
ID  PaymentDate   
1   18-01-01
1   18-02-03
2   18-04-03
2   18-05-08
2   18-06-06
3   17-12-23
3   18-01-22
3   18-02-24
4   17-11-09
4   18-12-06
", header=T)

library(dplyr)
library(lubridate)

df %>%
  group_by(ID) %>%
  mutate(ActivationDate = min(ymd(PaymentDate))) %>%
  ungroup()

# # A tibble: 10 x 3
#     ID PaymentDate ActivationDate
#   <int> <fct>       <date>        
# 1     1 18-01-01    2018-01-01    
# 2     1 18-02-03    2018-01-01    
# 3     2 18-04-03    2018-04-03    
# 4     2 18-05-08    2018-04-03    
# 5     2 18-06-06    2018-04-03    
# 6     3 17-12-23    2017-12-23    
# 7     3 18-01-22    2017-12-23    
# 8     3 18-02-24    2017-12-23    
# 9     4 17-11-09    2017-11-09    
#10     4 18-12-06    2017-11-09 

假设您的数据集已经订购,并且您不想使用Date格式,则可以使用

df %>%
  group_by(ID) %>%
  mutate(ActivationDate = first(PaymentDate)) %>%
  ungroup()

答案 1 :(得分:1)

使用data.table

的解决方案

数据:

df1<-
        fread("ID  Payment
    1   18-01-01
    1   18-02-03
    2   18-04-03
    2   18-05-08
    2   18-06-06
    3   17-12-23
    3   18-01-22
    3   18-02-24
    4   17-11-09
    4   18-12-06") %>% setDF

代码:

data.table::setDT(df1)[,Activation := Payment[1],by="ID"][]

结果:

 #   ID  Payment Activation
 #1:  1 18-01-01   18-01-01
 #2:  1 18-02-03   18-01-01
 #3:  2 18-04-03   18-04-03
 #4:  2 18-05-08   18-04-03
 #5:  2 18-06-06   18-04-03
 #6:  3 17-12-23   17-12-23
 #7:  3 18-01-22   17-12-23
 #8:  3 18-02-24   17-12-23
 #9:  4 17-11-09   17-11-09
#10:  4 18-12-06   17-11-09

快速提示:

  • 永远不要在列名中再次使用“空格”
  • 使用下划线或驼峰大写。例如:payment_datepaymentDate

答案 2 :(得分:1)

使用sqldf

您的数据集:

df=read.table(text="ID  PaymentDate   
          1   18-01-01
          1   18-02-03
          2   18-04-03
          2   18-05-08
          2   18-06-06
          3   17-12-23
          3   18-01-22
          3   18-02-24
          4   17-11-09
          4   18-12-06",header=T)

代码

# we can first find the minimum PaymentDate using the inner query and then
# populate the data.frame using the inner query
sqldf("select a.ID,a.PaymentDate, b.ActivationDate from df as a JOIN 
 (select ID,min(PaymentDate) as ActivationDate from df group by ID) as b where a.ID=b.ID")

输出:

   ID PaymentDate ActivationDate
1   1    18-01-01       18-01-01
2   1    18-02-03       18-01-01
3   2    18-04-03       18-04-03
4   2    18-05-08       18-04-03
5   2    18-06-06       18-04-03
6   3    17-12-23       17-12-23
7   3    18-01-22       17-12-23
8   3    18-02-24       17-12-23
9   4    17-11-09       17-11-09
10  4    18-12-06       17-11-09