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

前端 未结 2 571
说谎
说谎 2021-01-25 02:04

Starting from this SO question.

Example data.frame:

df = read.table(text = \'ID  Day Count   Count_group
            18  1933    6   15
             


        
2条回答
  •  误落风尘
    2021-01-25 02:09

    A generalised version of my previous answer...

    #first add grouping variables
    days <- 5 #grouping no of days
    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]

提交回复
热议问题