Group Data in R for consecutive rows

后端 未结 3 1851
故里飘歌
故里飘歌 2020-12-10 18:13

If there\'s not a quick 1-3 liner for this in R, I\'ll definitely just use linux sort and a short python program using groupby, so don\'t bend over

3条回答
  •  执念已碎
    2020-12-10 18:58

    with(df_in, {
      aggregate(day, list('ID'=ID, 'weight'=weight), 
                function(x) c('min_day' = min(x), 'max_day' = max(x)))
    })
    

    Produces:

      ID weight x.min_day x.max_day
    1  1    150         1         5
    2  1    151         3         3
    3  2    170         1         3
    4  2    171         4         5
    

提交回复
热议问题