2

I am looking for an R solution that can efficiently produce the output shown below. I can produce this easily in SAS with retain statement and a few lines of if-then-else logic, etc.. but I am not find anything similar on the Rforum or this site's archive. Below is the logic I am trying to apply to produce the output table below. Thanks in any help!

if the ID is the first ID encountered then group=1 and groupdate=date or else if not first ID and date - previous date > 10 or date - previous group date >10 then group=previous group # + 1 and groupdate = date or else if not first ID and date - previous date <= 10 or date - previous group date<=10 then group=previous group # and groupdate = previous date.

Input:

ID  DATE        ITEM
1   1/1/2014    P1
1   1/15/2014   P2
1   1/20/2014   P3
1   1/22/2014   P4
1   3/10/2015   P5
2   1/13/2015   P1
2   1/20/2015   P2
2   1/28/2015   P3
2   2/28/2015   P4
2   3/20/2015   P5

Desired Output

ID  DATE        ITEM    GROUP   GROUPDATE
1   1/1/2014    P1  1   1/1/2014
1   1/15/2014   P2  2   1/15/2014
1   1/20/2014   P3  2   1/15/2014
1   1/22/2014   P4  2   1/15/2014
1   3/10/2015   P5  3   3/10/2015
2   1/13/2015   P1  1   1/13/2015
2   1/20/2015   P2  1   1/13/2015
2   1/28/2015   P3  2   1/28/2015
2   2/28/2015   P4  3   2/28/2015
2   3/20/2015   P5  4   3/20/2015
3
  • 1
    Please check the GROUP for ID 2. It is not making much sense.
    – akrun
    Apr 18, 2016 at 6:15
  • The outpost table I wrote out is correct.. The problem is in my logic - I omitted a small part , hence I will update now .. Below is correct logic.
    – Pele
    Apr 18, 2016 at 17:58
  • if the ID is the first ID encountered then group=1 and groupdate=date or else if not first ID and date - previous date > 10 or date - previous group date >10 then group=previous group # + 1 and groupdate = date or else if not first ID and date - previous date <= 10 or date - previous group date<=10 then group=previous group # and groupdate = previous date.
    – Pele
    Apr 18, 2016 at 18:10

2 Answers 2

2

We can use data.table

library(data.table)
setDT(df1)[, GROUP:={
         dt <- as.Date(DATE, "%m/%d/%Y")
         gr1 <-cumsum((dt-shift(dt, fill=dt[1L]))>10)+1L; list(gr1)} ,
            by =  ID]
df1[, GROUPDATE := DATE[1L] , by = .(GROUP, ID)]
df1
#    ID      DATE ITEM GROUP GROUPDATE
# 1:  1  1/1/2014   P1     1  1/1/2014
# 2:  1 1/15/2014   P2     2 1/15/2014
# 3:  1 1/20/2014   P3     2 1/15/2014
# 4:  1 1/22/2014   P4     2 1/15/2014
# 5:  1 3/10/2015   P5     3 3/10/2015
# 6:  2 1/13/2015   P1     1 1/13/2015
# 7:  2 1/20/2015   P2     1 1/13/2015
# 8:  2 1/28/2015   P3     1 1/13/2015
# 9:  2 2/28/2015   P4     2 2/28/2015
#10:  2 3/20/2015   P5     3 3/20/2015
1
  • Hi akrun, I have update the logic for the table to utilize the newly created group date ( see above). How do I incorporate that in your code? Thanks for help!
    – Pele
    Apr 19, 2016 at 0:36
0

Here is an alternative method for it:

df <- read.table(header=T,text='ID  DATE        ITEM
               1   1/1/2014    P1
               1   1/15/2014   P2
               1   1/20/2015   P3
               1   1/22/2015   P4
               1   3/10/2015   P5
               2   1/13/2015   P1
               2   1/20/2015   P2
               2   1/28/2015   P3
               2   2/28/2015   P4
               2   3/20/2015   P5')

df$DATE <- as.Date(df$DATE,"%m/%d/%Y")

split.rows <- split.default(1:nrow(df),df$ID,drop=T)

lapply(split.rows,function(x){
split_df <- df[x,]

group <- vector('integer',length(x))
group_date <- vector('character',length(x))

group[1] <- 1
group_date[1] <- as.character(split_df[1,'DATE'])

for (i in 2:nrow(split_df)){
  if (split_df[i,'DATE'] - split_df[i-1,'DATE'] >= 10){
    group[i] <- group[i - 1] + 1
    group_date[i] <- as.character(split_df[i,'DATE'])
  }
  else{
    group[i] <- group[i - 1]
    group_date[i] <- group_date[i-1]
  }
}

df$GROUP[x] <<- group
df$GROUPDATE[x] <<- group_date

return(NULL)
})

> df
ID       DATE ITEM GROUP  GROUPDATE
1   1 2014-01-01   P1     1 2014-01-01
2   1 2014-01-15   P2     2 2014-01-15
3   1 2015-01-20   P3     3 2015-01-20
4   1 2015-01-22   P4     3 2015-01-20
5   1 2015-03-10   P5     4 2015-03-10
6   2 2015-01-13   P1     1 2015-01-13
7   2 2015-01-20   P2     1 2015-01-13
8   2 2015-01-28   P3     1 2015-01-13
9   2 2015-02-28   P4     2 2015-02-28
10  2 2015-03-20   P5     3 2015-03-20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.