Alex Li
Alex Li

Reputation: 13

Assignning dates according to categorical variable

I have a set of Payment Date in the following form:

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

I would like to add a column, Activation Date, as the earliest payment date for each ID, like:

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

I am wondering instead of going into a loop and take care of each ID one by one, there must be a much more clever way to do this.

Upvotes: 1

Views: 127

Answers (3)

Saurabh Chauhan
Saurabh Chauhan

Reputation: 3221

Using sqldf:

Your dataset:

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)

Code

# 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")

Output:

   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

Upvotes: 1

AntoniosK
AntoniosK

Reputation: 16121

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 

Assuming that your dataset is already ordered and you don't want to use Date formats, you can use

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

Upvotes: 1

Andre Elrico
Andre Elrico

Reputation: 11490

Solution using data.table

data:

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

code:

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

result:

 #   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

quick tipp:

  • Never use "spaces" in column names again
  • use underscore or camelcase. eg: payment_date, paymentDate

Upvotes: 1

Related Questions