[R] Creating two new variables conditional on retaining values from previous rows

pele.s at yahoo.com pele.s at yahoo.com
Wed Apr 20 00:59:41 CEST 2016


Hello,

Iam 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 archives. 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

Thanks for any help!



More information about the R-help mailing list