Aggregate by factor levels, keeping other variables in the resulting data frame

前端 未结 5 2086
太阳男子
太阳男子 2020-12-05 18:45

I\'m trying to calculate the minimum values of a numeric column for each level of a factor, while keeping values of another factor in the resulting data frame.



        
5条回答
  •  一个人的身影
    2020-12-05 19:30

    You need to use merge on result of aggregate and original data.frame

    merge(aggregate(value ~ code, dat, min), dat, by = c("code", "value"))
    ##   code value   index
    ## 1 HH11  24.1  023434
    ## 2 HH45  37.2 3377477
    ## 3 JL03  20.0 1177777
    

提交回复
热议问题