Expand data.frame by creating duplicates based on group condition (2)

℡╲_俬逩灬. 提交于 2019-12-02 09:38:24

Here is a solution that keeps the ID values as above.

#first add grouping variables
df$smalldaygroup <- c(0,cumsum(sapply(2:nrow(df),function(i) df$Day[i]!=df$Day[i-1]))) #individual days
df$bigdaygroup <- c(0,cumsum(sapply(2:nrow(df),function(i) df$Day[i]<df$Day[i-1]-1))) #blocks of consecutive days

#duplicate individual days except the first in each big group
df2 <- lapply(split(df,df$bigdaygroup),function(x) 
  split(x,x$smalldaygroup)[c(1,rep(2:length(split(x,x$smalldaygroup)),each=2))])

#change the Count_group to previous value in alternate entries
df2 <- lapply(df2,function(L) lapply(1:length(L),function(i) {
  x <- L[[i]]
  if(!(i%%2)) x$Count_group <- L[[i-1]]$Count_group[1]
  return(x)
}))

df2 <- do.call(rbind,unlist(df2,recursive=FALSE)) #bind back together

head(df2,20) #ignore rownames!
       ID  Day Count Count_group
01.1   18 1933     6          11
01.2   33 1933     6          11
01.3   37 1933     6          11
01.4   18 1933     6          11
01.5   16 1933     6          11
01.6   11 1933     6          11
02.7  111 1932     5          11
02.8   34 1932     5          11
02.9   60 1932     5          11
02.10  88 1932     5          11
02.11  18 1932     5          11
03.7  111 1932     5           8
03.8   34 1932     5           8
03.9   60 1932     5           8
03.10  88 1932     5           8
03.11  18 1932     5           8
04.12  33 1931     3           8
04.13  13 1931     3           8
04.14  56 1931     3           8
05.12  33 1931     3           4
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!