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

前端 未结 5 2091
太阳男子
太阳男子 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:28

    If you had added in the index variable that would have done it.

    library(plyr)
    
    # ddply
    ddply(dat, .(code,index), summarise, val = min(value))
    
    # base R
    aggregate(value ~ code + index, dat, min)
    

提交回复
热议问题