Here an example of my data.frame:
df = read.table(text = 'ID Day Count Count_group
1001 1933 6 11
1002 1933 6 11
1003 1933 6 11
1004 1933 6 11
1005 1933 6 11
1006 1933 6 11
1007 1932 5 8
1008 1932 5 8
1009 1932 5 8
1010 1932 5 8
1011 1932 5 8
1012 1931 3 4
1013 1931 3 4
1014 1931 3 4
1015 1930 1 1
1016 1800 6 10
1017 1800 6 10
1018 1800 6 10
1019 1800 6 10
1020 1800 6 10
1021 1800 6 10
1022 1799 4 6
1023 1799 4 6
1024 1799 4 6
1025 1799 4 6
1026 1798 2 2
1027 1798 2 2
1028 888 4 6
1029 888 4 6
1030 888 4 6
1031 888 4 6
1032 887 2 3
1033 887 2 3
1034 886 1 2
1035 885 1 1', header = TRUE)
The Count
col shows the total number of ID
values per each Day
and the Count_group
col shows the sum of the ID
values per each Day
and Day - 1
.
e.g. 1933 = Count_group
11 because Count
6 (1933) + Count
5 (1932), and so on.
What I need to do is to create duplicated observations per each Count_group
and add them to it in order to show per each Count_group
its Day
AND Day - 1
.
e.g. Count_group
= 11 is composed by the Count
values of Day
1933 and 1932. So both days needs to be included in the Count_group
= 11.
The next one will be Count_group = 8, composed by 1932 and 1931, etc...
Expected output:
ID Day Count Count_group
1001 1933 6 11
1002 1933 6 11
1003 1933 6 11
1004 1933 6 11
1005 1933 6 11
1006 1933 6 11
1007 1932 5 11
1008 1932 5 11
1009 1932 5 11
1010 1932 5 11
1011 1932 5 11
1007 1932 5 8
1008 1932 5 8
1009 1932 5 8
1010 1932 5 8
1011 1932 5 8
1012 1931 3 8
1013 1931 3 8
1014 1931 3 8
1012 1931 3 4
1013 1931 3 4
1014 1931 3 4
1015 1930 1 4
1015 1930 1 1
1016 1800 6 10
1017 1800 6 10
1018 1800 6 10
1019 1800 6 10
1020 1800 6 10
1021 1800 6 10
1022 1799 4 10
1023 1799 4 10
1024 1799 4 10
1025 1799 4 10
1022 1799 4 6
1023 1799 4 6
1024 1799 4 6
1025 1799 4 6
1026 1798 2 6
1027 1798 2 6
1026 1798 2 2
1027 1798 2 2
1028 888 4 6
1029 888 4 6
1030 888 4 6
1031 888 4 6
1032 887 2 6
1033 887 2 6
1032 887 2 3
1033 887 2 3
1034 886 1 3
1034 886 1 2
1035 885 1 2
1035 885 1 1
Do you have any suggestion?