Expand data.frame by creating duplicates based on

2019-08-22 18:39发布

问题:

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?

回答1:

I think this does what you need...

#first add a grouping variable
df$daygroup <- c(0,cumsum(sapply(2:nrow(df),function(i) df$Day[i]!=df$Day[i-1])))

#split df into a list of data frames, loop through them to add extra rows, 
#and bind them back together
df2 <- do.call(rbind,lapply(split(df,df$daygroup),function(x){ 
  n <- nrow(x)
  m <- x$Count_group[1] #number of rows needed for Day
  if(m>n){
    y <- rbind(x,data.frame(ID=(x$ID[n]+1):(x$ID[n]+m-n), #continue numbering
                            Day=x$Day[1]-1, #previous day
                            Count=m-x$Count[1], #difference in count
                            Count_group=m,
                            daygroup=x$daygroup[1]))
  } else {
    y <- x #no extra rows needed
  }
  return(y)
}
))
df2$daygroup <- NULL #remove grouping variable


head(df2,20) #ignore the rownames!
       ID  Day Count Count_group
0.1  1001 1933     6          11
0.2  1002 1933     6          11
0.3  1003 1933     6          11
0.4  1004 1933     6          11
0.5  1005 1933     6          11
0.6  1006 1933     6          11
0.7  1007 1932     5          11
0.8  1008 1932     5          11
0.9  1009 1932     5          11
0.10 1010 1932     5          11
0.11 1011 1932     5          11
1.7  1007 1932     5           8
1.8  1008 1932     5           8
1.9  1009 1932     5           8
1.10 1010 1932     5           8
1.11 1011 1932     5           8
1.1  1012 1931     3           8
1.2  1013 1931     3           8
1.3  1014 1931     3           8
2.12 1012 1931     3           4