lundi 1 mai 2017

Create 100 data.frames with dates grouped randomly with a fixed number of days

Here an example of my data.frame and let's assume that the date col represents dates:

df = read.table(text = 'ID date
a 1
a 2
a 3
a 4
a 7
a 11
b 1
b 3
b 4
b 10
b 11', header = TRUE)

So, my dates here range from 1 to 11 and I would like to create 100 data.frames where for each one the date (and ID) col will be grouped randomly by 3 subsequent dates.

e.g.

df1

ID date group
    a 1 1
    a 2 1
    a 3 1
    a 4 2
    a 7 2
    a 11 2
    b 1 3
    b 3 3
    b 4 3
    b 10 4
    b 11 4

df2

ID date group
    a 1 4
    a 2 1
    a 3 1
    a 4 1
    a 7 2
    a 11 2
    b 1 2
    b 3 3
    b 4 3
    b 10 3
    b 11 4

df3

ID date group
    a 1 3
    a 2 3
    a 3 3
    a 4 4
    a 7 4
    a 11 1
    b 1 1
    b 3 1
    b 4 2
    b 10 2
    b 11 2

etc...

Do you have any suggestion? Thanks




Aucun commentaire:

Enregistrer un commentaire