Grouping of R dataframe by connected values

前端 未结 4 685
臣服心动
臣服心动 2021-01-13 02:07

I didn\'t find a solution for this common grouping problem in R:

This is my original dataset

ID  State
1   A
2   A
3   B
4   B
5   B
6   A
7   A
8            


        
4条回答
  •  误落风尘
    2021-01-13 02:41

    Here is another attempt using rle and aggregate from base R:

    rl <- rle(df$State)
    newdf <- data.frame(ID=df$ID, State=rep(1:length(rl$lengths),rl$lengths))
    newdf <- aggregate(ID~State, newdf, FUN = function(x) c(minID=min(x), maxID=max(x)))
    newdf$State <- rl$values
    
      # State ID.minID ID.maxID
    # 1     A        1        2
    # 2     B        3        5
    # 3     A        6        8
    # 4     C        9       10
    

    data

    df <- structure(list(ID = 1:10, State = c("A", "A", "B", "B", "B", 
    "A", "A", "A", "C", "C")), .Names = c("ID", "State"), class = "data.frame", 
    row.names = c(NA, 
        -10L))
    

提交回复
热议问题