The question is similar to (Update) Add index column to data.frame based on two columns
Here my example data.frame:
df = read.table(text = 'ID Day Count Count_group
77661 14498 4 5
76552 14498 4 5
37008 14498 4 5
34008 14498 4 5
30004 14497 1 5
30004 14497 1 4
28047 14496 3 4
28049 14496 3 4
29003 14496 3 4
69012 14468 1 4
69007 14467 3 4
69012 14467 3 4
69020 14467 3 4
42003 13896 2 4
42011 13896 2 4
22001 13895 2 4
23007 13895 2 4
28047 14496 3 3
28049 14496 3 3
29003 14496 3 3
69007 14467 3 3
69012 14467 3 3
69020 14467 3 3
48005 14271 2 2
48007 14271 2 2
22001 13895 2 2
23007 13895 2 2
47011 14320 1 2
73005 14319 1 2
73005 14319 1 1', header = TRUE)
The Count
col shows the sum of the ID
values grouped by Day
.
The Count_group
shows the sum of the unique Count
vales grouped by Day
and Day -1
.
I need to create an index column which groups the Count_group
by Day
and Day -1
following the descending order of the df
(with duplicates!).
Here my expected output:
ID Day Count Count_group index_col
77661 14498 4 5 1
76552 14498 4 5 1
37008 14498 4 5 1
34008 14498 4 5 1
30004 14497 1 5 1
30004 14497 1 4 2
28047 14496 3 4 2
28049 14496 3 4 2
29003 14496 3 4 2
69012 14468 1 4 3
69007 14467 3 4 3
69012 14467 3 4 3
69020 14467 3 4 3
42003 13896 2 4 4
42011 13896 2 4 4
22001 13895 2 4 4
23007 13895 2 4 4
28047 14496 3 3 5
28049 14496 3 3 5
29003 14496 3 3 5
69007 14467 3 3 6
69012 14467 3 3 6
69020 14467 3 3 6
48005 14271 2 2 7
48007 14271 2 2 7
22001 13895 2 2 8
23007 13895 2 2 8
47011 14320 1 2 9
73005 14319 1 2 9
73005 14319 1 1 10
And do the same but with index_col
grouping by 3 days: Day
, Day -1
and Day -2
:
df_2 = read.table(text = 'ID Day Count Count_group
30004 14497 1 5
28047 14496 3 5
28049 14496 3 5
29003 14496 3 5
69012 14495 1 5
69007 14467 3 5
69012 14467 3 5
69020 14467 3 5
42003 14466 1 5
42011 14465 1 5
28047 14496 3 4
28049 14496 3 4
29003 14496 3 4
69012 14995 1 4
22001 13895 2 4
23007 13895 2 4
28047 13894 2 4
28049 13894 2 4
42003 14466 1 2
42011 14465 1 2
28047 13894 2 2
28049 13894 2 2
69012 14995 1 1
42011 14465 1 1', header = TRUE)
Expected output:
ID Day Count Count_group index_col
30004 14497 1 5 1
28047 14496 3 5 1
28049 14496 3 5 1
29003 14496 3 5 1
69012 14495 1 5 1
69007 14467 3 5 2
69012 14467 3 5 2
69020 14467 3 5 2
42003 14466 1 5 2
42011 14465 1 5 2
28047 14496 3 4 3
28049 14496 3 4 3
29003 14496 3 4 3
69012 14995 1 4 3
22001 13895 2 4 4
23007 13895 2 4 4
28047 13894 2 4 4
28049 13894 2 4 4
42003 14466 1 2 5
42011 14465 1 2 5
28047 13894 2 2 6
28049 13894 2 2 6
69012 14995 1 1 7
42011 14465 1 1 8
Do you have any suggestion? I desire to create a generic code that could be applied (with a few adjustments) to both df, df_2 and to other data.frames with grouping variable of n days.