lundi 8 mai 2017

Shift rows by group by random generated delta t

Here an example of my data.frame:

df = read.table(text = 'ID Day
101 0
101 0
101 1
300 40
300 40
270 32
270 35
100 1450
100 1450
100 1451
72 1490
72 1499', header = TRUE)

Assuming that each Day obs represents a real date and that the time period ranges from Day = 0 to Day = 1500, I need to shift each ID group by a random amount of Day (delta t), specific for each group and if the Day value passes 1500 start again from 0.

e.g. group 101 by 30 Day, group 300 by 70, group 270 by 1000, group 100 by 100 and group 72 by 5

Here my desired output:

     ID Day
    101 30
    101 30
    101 31
    300 110
    300 110
    270 1032
    270 1035
    100 1550
    100 1550
    100 1551
    72 1495
    72 3

Note that the shift in Day per each group has to be random.

Any help would be really appreciated.

Thanks




Aucun commentaire:

Enregistrer un commentaire